.net - LINQ Sample: Select Typed Objects From A List
Jul 7, 2011
I have
Dim objectsList as List(Of Object) = GetAllObjects()
' Filter from Objects just Persons '
Dim peopleList as List(Of Person) = ???
What is the most efficient and effective LINQ expression to do it?
EDIT
1 Dim selectedObjects As List(Of Object) = GetAllObjects()
2 Dim selectedPeople As IEnumerable(Of Person)= selectedObjects.OfType(Of Person)
3 Dim people As List(Of Person) = selectedPeople.ToList()
Error on 3:
Value of type
'System.Collections.Generic.List(Of
System.Collections.Generic.IEnumerable(Of Person))' cannot be
converted to
'System.Collections.Generic.List(Of Person)'.
View 3 Replies
ADVERTISEMENT
Mar 1, 2011
Can I use LINQ to return an item in a collection that is the same TYPE as the items in the collection? I am a LINQ Noob trying to avoid looping.
Dim mostRecentlyCreatedQuestionnaire As ParentQuestionnaireItem =
CType((From P In questionnaireCollection Order By P.Metadata.CreateDate Descending).Take(1), ParentQuestionnaireItem)
I get an "unable to CAST" error when I include the CTYPE function. I kind of expected that error, but I imagine that if I coul dnot do this, LINQ's usefulness would be diminished and therefore assume that there must be a way..
View 2 Replies
Dec 22, 2010
I have 2 classes
Public Class Shipper
Public Property ShipperID As Long
Public Property CreateDate As DateTime
[Code].....
View 1 Replies
Nov 23, 2011
I'm trying to create a list of days(integer) from a list of dates(date).
I tryed to do this....
[Code]...
View 4 Replies
Mar 1, 2011
I have list object and I need to check id in a comma separated string using LINQ in VB.NET, something like this:
dim strId as String = "1,2,3,5,"
dim myList = from objmylist where objmylist.id in (strId)
View 4 Replies
Oct 11, 2011
I have a list of objects, each with 2 relevant properties: "ID" and "Name". Lets call the list "lstOutcomes".I need to check the list for duplicates (meaning object1.ID = object2.ID, etc.) and set a flag (valid = false, or something) if there is at least one duplicate. Also, it would be nice to send a message to the user mentioning the "Name" of the object, when it fails.I am sure I will need to use the Group By operator to do this, but I am not used to doing that in LINQ, and the examples out there are just not helping me. This article seems to be close to what i need, but not quite and it's in C#.
Here is a starting stab at it...
Dim duplist = _
(From o As objectType In lstOutcomes _
[code]......
help?
View 4 Replies
May 3, 2012
I have a query that 'selects' object of a type:
Dim l as IList(Of Foo) = (From dataRow As DataRow In table.Select()
Where CStr(dataRow("Column1")) = "A"
Select New Foo(CStr(dataRow("Column1")), _
CStr(dataRow("Column2")))).ToList()
What's happening is that if i set a break-point to the constructor of Foo and step, the constructor is hit and the parameters are loaded with the arguments. However, l has empty Foo objects (the members in every object are Nothing). What could be happening here?
View 1 Replies
Apr 27, 2012
I figure I should use 'Aggregate' but apparently I am getting it wrong First I get a list of my Entity objects Dim employers As List(Of myEntity) = (New XXXX()).getZZZ(userName, userType) Then I figured this would be a way to put all the names in a string Dim names as String = employers.Aggregate(Function(current, [next]) current.Name & " " & [next].Name)
[Code]....
View 2 Replies
Feb 3, 2010
Basically, I am trying to write a LINQ to Objects statement where the relationship is a grandparent, parent, child relationship. (You could also call it a Master Detail relationship.)
In Legacy code here is a simplified version what I am trying to accomplish.
Dim coverages As New List(Of Coverage)
Dim coverage As Coverage
For Each rl In oClaimsPolicy.RiskLocations
coverage = New Coverage
coverage.Level = "Location"
[Code] .....
If is it not clear one Location can have many Items and one Item can have many Coverages. I basically want to list the items and show the relationship between grandparent (Location), parent (Item) and child (Coverage).
View 5 Replies
Jul 3, 2011
I am having so trouble with the ToUpper() procedure with strings. I am using LINQ to find unique values in a list of objects. In that whole process I set the values to compare to all lowercase in order to get a proper list. My goal is to populate a dropdown list with the values. I'm attempting to set the first letter of of the string to uppercase and keep the rest lowercase however the toUpper() procedure is not working?
Private Function FixCase(ByVal strIn) As String
Dim strOutput As String
Dim intStringLength As Integer = strIn.Length - 1
strOutput = strIn.Substring(0, 1)
[code].....
View 5 Replies
Aug 31, 2011
I don't know if this is doable, maybe with Linq, but I have a List(Of MyType):
Public Class MyType
Property key As Char
Property description As String
End Class
And I want to create a Dictionary(Of Char, MyType) using the key field as the dictionary keys and the values in the List as the dictionary values, with something like:
New Dictionary(Of Char, MyType)(??)
Even if this is doable, internally it will loop through all the List items, I guess?
View 3 Replies
Nov 16, 2011
I have a combo box that is populated with a list of objects (corresponding to all employee rows from a database that meet the criteria IsTech). These are not entity objects though, I have created my own class that stores the member values of each row from the DB. I have set the DisplayMemberPath to EmployeeId so the Combo box display the EmployeeId ( an integer ) member of my object. Ie. right now there are only 2 IsTech employees in my DB so the combobox displays the numbers 3 and 8 ( their EmployeeIDs).
[Code]...
View 3 Replies
Mar 24, 2011
I have two lists declared as follows:
Dim lstDBItems As New List(Of DBItem)
Dim lstAppItems As New List(Of AppItem)
I am trying to do something like this:
I've a function which returns List(Of AppItem):
Function GetAppItems() As List(Of AppItem)
'...
End Function
In the above function I populate lstDBItems and then write the return statement like follows:
Return lstDBItems.Select(Function(x)
dim oItem As New AppItem()
oItem.Property1 = x.DbProperty1
[Code]....
The weird thing is the code compiles, but on rumtime I get a type case error.
View 3 Replies
Feb 17, 2012
I have a list of "ObjDay" which could have replicated items. How can I select them?
ObjDay is:
Public class ObjDay
_date
...
End Class
[Code].....
View 1 Replies
May 4, 2012
This is my function:
Public Function GetAllEmployee() As List(Of Employees)
Return DB.Employees.Select(Function(q) New With {q.EmployeeID, q.LastName,q.FirstName}).ToList()
End Function
I'm getting an error:
Value of type System.Collections.Generic.List(Of <anonymous type>) cannot be converted to System.Collections.Generic.List(Of NorthwindModel.Employees).
View 1 Replies
May 25, 2010
I have a query that can be summarised in SQL as follows;
Select
S.StockCode
From
StockToCheck As S
[Code]....
Where the S var is a list of strings. This gives the error 'S' is not declared and points to the S in the function call / join (GetPOSStock). So it does not seem possible to do this in Linq, can anyone confirm?
View 1 Replies
Apr 7, 2010
I am hoping that somebody here is familiar with the book, "Doing Objects with Visual Basic 2005" so this question makes more sense but I will try and ask it in a way that makes knowledge of the book irrelevant.The book contains a sample project that I am trying to use as a "framework" for all my new projects. The project shows you how to read and edit objects to and from a database and that part works great. The author then leaves the new and deletes up to the reader to do, which is a good way to learn. However I am having trouble with the it. My problem is that I am having trouble implementing my own ProcessNew InterfaceI think this is how it should be:
Public Function ProcessNew() As Boolean Implements IMDIChild.ProcessNew
Dim myGoal As Goal = TryCast(GoalBindingSource.AddNew, Goal)
myGoal.Goal = rtxtGoals.Text
[code].....
View 8 Replies
Nov 3, 2009
I am looking for sample linq code snippet which uses System.Linq.Dynamic against a datatable.
Dim entities = (From ent In dt.AsEnumerable().Where(String.Format("IsUSFederal == {0}", "true")) _
Select Description = ent("Description"), Acronym = ent("Acronym")).ToList
I am getting an error "there is no accessible Where can be called with these arguments". I have included the DynamicLinq.vb file and the application compiles fine (aside from this error). I have included Imports System.Linq.Dynamic but it doesn't appear to be working.
View 1 Replies
May 5, 2010
can anyone give a sample code for select case statements under a button click event?
View 2 Replies
Apr 18, 2011
Is this the most straight forward way to return a typed collection?Here's the key line of code which returns a implicit type of IEnumerable that used to manually loop through to manually recreated a TYPED collection. Is there any native LINQ way to return a typed sorted collection without this recreating of the collection? [code]
View 2 Replies
Apr 24, 2009
I am binding a Winforms Grid to an entity. (For reasons I won't go into here it must be bound to the entity, not the result a query) The code is as follows:
grid.DataSource = myEntities.entityName.Where("it.field = " & field)
It works, but it obviously isn't strongly typed. Is there a way to define the Where clause of an entity using a strongly typed notation?
View 1 Replies
Jan 25, 2010
I'm trying to do a LINQ Query against a typed datatable that will return me duplicated data.With out going into too much detail, here's the basic setup:
strCode1 - string type
intCode1 - int32 type
intAdjustor - int32
There are more fields but these are the ones that are important.I'm going to do my best to describe this, as it's kind of confusing.In theory intCode1 is a key for strCode1 (hence the names). So each time intCode1 is duplicated, the same strCode1 should appear. Example:
[code]...
Ultimately I'd like to get a List(Of T) - where T is my typed data row. In SQL, I would simply use a sub query, to get a distinct list of strCode1 and intCode1, then do a count, grouped by strCode1 where I get more than one row.I'm not sure how to translate that into LINQ, AND get the original types data rows returned in a list.
View 7 Replies
Aug 24, 2009
How do I change this code so that if someone didnt select something and typed in something themselfs, they will get this msg?
If ComboBox1.Text = {Else} Then
MsgBox("Please select something.")
End If
View 3 Replies
Dec 17, 2010
I have the following list Dim ordersToUpdate as New List(Of OrderUpdate)I am using a "For Each" to iterate and update a legacy DB..I am having a LOCKING problem on the DB when the "ordersToUpdate" is large. Instead to puting a Threading.Thread.Sleep to pause, is there a proper way to break the list up into sections e.g iterating 100 at a time?
View 1 Replies
Mar 14, 2012
How can I "search" through a strongly typed list for a string?
I am attempting .Contains(TheString), however it errors stating Unable to cast object of type 'System.String' to type 'o7thCrawler.Typing.ImportantTyping'
Here is the code:
Public Class LinkTyping
Public Property Url As String
Public Property Title As String
[Code]....
View 3 Replies
Jul 1, 2010
I have a combo box that contains data from a database.I am using autocomplete to assist users in finding the large amount of data that the combo box is bound to.What I need to do is check the typed data with the values bound to the box.I need to show a message box if what the user types isn't in the list. Right now if its not in the list and they tab out of the box it throws an exception.Is there a way to validate what is typed with the data in the list. I tried changing the drop down style to dropdownlist but this doesn't allow the user to type in data.
View 3 Replies
Jun 2, 2011
I would like to create list of child objects from list of parent object. Like If i have list of bookingroom which has one member room then i would like to create list of room from it.
eg. code:
Dim BookingRoomList As List(Of BookingRoom) = New List(Of BookingRoom)
Dim RoomList As List(Of Room) = New List(Of Room)
BookingRoomList = BookingRooms.FillGrid()
[Code]....
Is there any short cut method instead of iterating over for earch?
View 1 Replies
Jul 20, 2011
I'm having a bit of a 'brain doner' moment here
I have a list of Objects. Each of these Objects contains a list of other Objects (pseudo code) :-
Private Structure Object1
dim Name as string
dim ListOfObject2 as List(Of Object2)
[Code]....
I need to sort the list of Object1 by the Value in Object2. I have a comparison class which sorts Object2 by its Value nicely.
View 13 Replies
Feb 21, 2009
In which situations I should use LINQ to Objects?
Obviously I can do everything without LINQ. So in which operations LINQ to code shorter and/or more readable?
View 7 Replies
Jun 5, 2012
I have a linq query like that:
Elementos = From b In Elementos Where b.Value.IdGrupo = 0 Select b
"Elementos" is a dictionary(of long, MyObject). MyObject has approximately 50 properties.
The problem I have is that I have a new requirement to accept string parameter with a "where" condition. Something like "property1>10 and property2 like 'anystring' or property3<=25". (That means any property could have a condition. I have string and numeric properties.)
I'd like to keep using linq, adding the condition as it comes. I'm aware that it could produce an exception if the condition is misspelled or something, but that's acceptable (by try catchs). I wouldn't want to parse the string to build parameters or anything.
View 2 Replies