Failure To Infer Type From Anonymous Method?
Jan 24, 2011
(using VB 08!) In order to avoid "magic strings", I have used this handy method (not my own!) many times to get a property's name as a string:
Function GetPropertyName(Of T, R)(ByVal expression As
Linq.Expressions.Expression(Of Func(Of T, R))) As
String
[Code].....
View 1 Replies
ADVERTISEMENT
Jul 9, 2009
I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures.
[Code]...
View 2 Replies
Apr 6, 2012
In the following code,
For Each item in MyCollection
...
Next
What does the compiler use to determine the type of item?For example let say I have this class, which is inheriting a non generic collection,
Public Class BaseDataObjectGenericCollection(Of T)
Inherits BaseDataObjectCollection
End Class
A for each loop still infers the Item type as Object. How would I have to modify the above class to make the type inference work?
Edit: Per Beatles1692's answer, Implementing IEnumerator(Of T) kinda works. The base class already has a GetEnumerator function, inherited from CollectionBase, so I my implementation looked like this,
Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return MyBase.Cast(Of T)().GetEnumerator
End Function
However, the for loop still infers the type as object. But, if I change the interface implementation to this,
Public Shadows Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return MyBase.Cast(Of T)().GetEnumerator
End Function
That works, the for loop gets the type inference correct. So I guess the question is now, does For Each just look for a function called GetEnumerator ?
View 3 Replies
Sep 24, 2011
I am sure i am doing something terribly wrong, but i should better ask the experts.
At the third line i get the error Value of type <anonymous type> cannot be converted to <anonymous type>
Dim Query = (From c In Db.web Select New With {.AA = c.AA}).ToList
Dim v = New With {.Amount = 108}
Query.Add(v)
View 2 Replies
Aug 30, 2010
I've pieced together some information from other posts but I'm stuck. The first part works fine. Basically I query the database using LINQ and then I loop through the results generating a report.
[Code]...
View 2 Replies
Mar 20, 2012
I have the following in C#: public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
[Code]...
View 2 Replies
Jan 25, 2012
There are two ways to create methods on the fly. Through Anonymous method and through Lambda expression (relevant for C# language as far as i know). Though there is not much difference between them. How to create Anonymous methods in VB.NET ?
View 1 Replies
Jul 13, 2011
I'm trying to create an extension method that returns an IEqualityComparer based on a lambda function. Heres the extension method:
<Extension()>
Public Function Comparer(Of T)(Func As Func(Of T, T, Boolean)) As IEqualityComparer(Of T)
Return New GenericComparer(Of T)(Func)
End Function
[Code]...
View 1 Replies
Sep 15, 2011
Calling method in anonymous object?This works:
Call (New MyForm).Show
View 13 Replies
Feb 23, 2010
How would you convert this to VB (using .NET 4.0 / VS2010) ?
bw.DoWork += (o, args) =>
{
Code Here
};
I thought maybe like this:
AddHandler bw.DoWork,
Function(o, args)[code]....
But it says Function does not return a value on all code paths.
View 6 Replies
Apr 18, 2012
With C# anonymous delegates, we can avoid declaration of extra method, I know in VB.NET they're not available, but is there a design pattern that I can use so that I can avoid writing an extra method each time? I need it for SPSecurity.RunWithElevatedPrivileges which will be used a lot of times in our code.
[Code]...
View 1 Replies
Apr 22, 2009
Imports System.Reflection
Public Class Test
Private Field As String
End Class
Module Module1
Sub Main()
Dim field = GetType(Test).GetField("Field", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
[Code]...
View 2 Replies
Oct 5, 2010
I found that this answer was asked and answered before VS2010 was actually released.[code]to a .NET Framework 4.0 project in Visual Studio 2010 and it does not compile.Do you now if this feature is really implemented and what I am doing wrong?
View 3 Replies
Feb 12, 2010
I am trying to figure out how to get at an anonymous typed object's properties, when that anonymous type isn't created in the current function.
Specifically, I am binding an ASP.NET ListView to LINQ resultset, then trying to process each item in the ItemDataBound event.
Option Explicit On
Option Strict On
Class MyPageClass
[Code]....
View 3 Replies
Nov 12, 2009
Why did the anonymous type property "Points" still have the value "0"? [code]
View 1 Replies
Oct 7, 2010
In MVC I can do something like the following to serialise an object with an anonymous type to JSON.[code]I'd like to do exactly the same but output xml. I haven't been able to find an equivalent method. Would someone please point me in the right direction?
View 2 Replies
Dec 1, 2010
I need to Convert a Ienumerable to a dataset. For this i need to write a common fnction to convert any IEnumberable type to Dataset. For this I need to set anonymous type.[code]...
View 1 Replies
May 27, 2012
I want to import an xls file to access by using the following code. My xls file has some numeric and some text cells when data be imported to the access table, the text cells won't be imported correctly.[code]
View 1 Replies
Oct 22, 2009
I'm out of my depth populating a windows form from an XML linq query (see code further down in post). The listbox is populated as intended, when a value is selected (row) I would like to populate the remaining form controls. The linq query I believe has created a new datatype "Anonymous Types". The query is only local to the load form subroutine but I'm not sure how to make it global.
When I define the query simply as an object the code in the selectedValueChanged event can't execute due to late databinding. how I can initially load the listbox from linq xml query and then populate the other controls when the value changes? Am I right in thinking even though the listbox is multi-columns (property) you can ONLY ever reference a row and NOT a column? I'm new to LINQ and Anonymous Types and the more I read the more confused I seem to get. On a less important note the order by is not functioning as intended (no errors) but data is always retrieved in xml file order and not by the node <text>. Also looked at using <text>.first to order the results without success.
[Code]...
View 5 Replies
Jun 23, 2009
I'd like to create a list of an anonymous type, for example:
Dim dsResource = New With {.Name = dsResourcesEnd(index).Last_Name & ", " & dsResourcesEnd(index).First_Name _
, .StartDate = dsResourcesStart(index).Day _
, .EndDate = dsResourcesEnd(index).Day}
I have created that anonymous type. Now I'd like to add it to a list of that type. How do I declare a list of that type?
View 4 Replies
Jun 22, 2011
I'm OK with both C# and VB.NET..I have a function GetListOfBook, that returns LINQ to SQL result that has a collection of objects like the following:[code]
View 4 Replies
Nov 20, 2009
The application works fine on Vista but throws this error on Windows 7:
- The type initializer threw an exception.
This is the code that causes it:
Public UserName As String = String.Empty
UserName = Environment.UserName
View 4 Replies
Aug 1, 2011
After reading this post i realized that i cannot pass an anonymous type as a parameter to a function. So what other options do i have?
Perhaps, passing a Query.ToList as parameter would help, or am i re-inventing the wheel?
Update: I have the following query, which i would like to pass to a function:
Sub Test
Dim Query = (From c In DB Select New With { .ElementName = c.Name })
DoSomething(Query)
[Code].....
View 3 Replies
Feb 25, 2010
I'm trying to write a linq to object query in vb.net, here is the c# version of what I'm trying to achieve (I'm running this in linqpad):
void Main()
{
var items = GetArray(
[code].....
View 1 Replies
Jan 5, 2012
I am using linq to fill a gridview with the information from an xml from code behind. I would like to order my Grid according to one of my elements in the xml ("value element").
gvResourceEditor.DataSource = (From resElem In resourceElements.Elements("data") _
Select New With { _
.Key = resElem.Attribute("name").Value, _
.Value = HttpUtility.HtmlEncode(resElem.Element("value").Value), _
.Comment = If(resElem.Element("comment") IsNot Nothing, HttpUtility.HtmlEncode(resElem.Element("comment").Value), String.Empty) _
}).OrderBy(?????)
View 1 Replies
Aug 10, 2010
Consider the requirement to always declare Option Strict On. We'll always need to declare variables with the As keyword. What would be the type of an anonymous type?Example : Dim product As ... = New With { Key .Name = "paperclips", .Price = 1.29 }
View 2 Replies
Nov 2, 2011
I have this if statement, which returns the result as a AsQueryable to an anonymous type:
If (signature = "") Then
testResults = (From TRTable In context.TestResults
Where ((TRTable.Art_no = currentProduct) And (TRTable.Server_time > startDate) And (TRTable.Server_time < endDate))
[code]....
I can't seem to figure out why it isn't working, it was working without the if statement, but I need that functionality.
View 1 Replies
Mar 27, 2012
I have the following Data Transfer Objects defined:
Public Class MemberWithAddressesDTO
Public Property Member_PK As Integer
Public Property Firstname As String
Public Property DefaultAddress As AddressDTO
Public Property Addresses As IQueryable(Of AddressDTO)
End Class
[Code]...
View 1 Replies
Dec 16, 2011
I'm learning LINQ and VB and just spent an hour trying to access the fields of an anonymous type defined in a linq query. The key (I learned) is to convert the query to a list before you try to iterate through it with a for loop. How to access property of anonymous type in C#?
This does not work: edit (this compiles, but intellisense does not recognize the type)[code]...
View 1 Replies
Sep 29, 2011
So I'm writing a query as follows:
Dim assSummary = From a In db.Assignments
Join ur In db.UserRegions
On a.Origin.ID Equals ur.Region.ID
[code]....
In the controller I can return the data easily as follows:
For Each c In assSummary
MsgBox(c.Description & " " & c.AssCount)
Next
If I pass the object through to the view using Viewdata("assSummary") = assSummary, how do I display the data? Every method I've tried results in messages about 'VB$AnonymousType_7(Of Integer,String) and I don't know how to retrieve the data from the anonymous type.
View 1 Replies