LINQ - Select A *TYPED* Item In A Collection?
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
ADVERTISEMENT
Jan 12, 2012
I have a parent child collection objects and was wondering how to get a single item from the child collection using Linq
[Code]...
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
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
Sep 8, 2010
Here is an example of my xml doc:
<?xml version="1.0" encoding="utf-8"?>
<employeedata>
<employee>
<firstname>Bob</firstname>
<lastname>Anderson</lastname>
[Code]...
However, only the first phone number (Office) displays in the listbox. The phoneinfo collection has both <phone> elements, but the For Each statement doesn't seem to iterate thru.
View 8 Replies
Oct 14, 2009
I cannot find how to display an error message when what I typed in the listbox field is not in the listbox collection.
For example the listbox contains the following items
1) Apple
2) Strawberry
3) Mango
If for example I type "Mongo" and then press tab to another field, a message will pop up saying I typed something not in the listbox. Anyone who knows how to code if text in textbox1 is not in listbox then blah blah blah.
View 1 Replies
Jun 8, 2012
I want to create a strongly typed multidimensional array or collection containing the following values from a database:[code]
View 3 Replies
Apr 15, 2009
I've got the following ADO.NET Entity Framework Entity Data Model:I want to find all the Policyholders with both a Service of a given Id and also a Keyword of a given Status.
This LINQ Does Not Work:
Dim ServicesId As Integer = ...
Dim KeywordStatus As Integer = ...
Dim FoundPolicyholders = From p As Policyholder In db.PolicyholderSet.Include("Keywords").Include("Services") _
Where p.Services.Id = ServicesId _
And p.Keywords.Status = KeywordStatus _
Select p
The Where clause cannot search the p.Services and p.Keywords EntityCollections in that way.
[Code]...
View 1 Replies
Apr 15, 2011
search the collection of a collection in my LINQ Where clause?
View 11 Replies
Oct 14, 2010
I am having a problem with the Visual Basic property collection editor .
I have created a Custom Type
Public Class Field
Private Item As Integer
Private Name As String = "FieldName"
[ode]....
View 1 Replies
Jul 5, 2010
In my application I have a listbox and SelectionMode should be MultiSimple because users need to see which items they selected. In another tab we have another listbox this one should show all the selection users had done in first tab. Private Sub
[Code]....
View 3 Replies
Apr 14, 2011
Take the following scenario:
Public Class Store
Public Overridable Property Areas As List(Of Area)
End Class
Public Class Area
[code]....
What's the quickest way to get a total count of shelves for a store? I.e. for a store I get the total count of areas by using Areas.Count Or will I need to loop through each area and tally the count of shelves?
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
Oct 13, 2011
How someone would select an item from a list box and click a button and have that item go into another listbox with it's price? I seem to be stuck on this part.
Here's my code:
I want to select the first option from the first listbox and I want to put it into another listbox along with the price of that item on the same line in the listbox. I can't seem to figure out how to that.
Public Class Form1
Const dcmPRICE_STRESS As Decimal = CDec(595.0)
Const dcmPRICE_TIME_MANAGMENT As Decimal = 695
[CODE]...
View 12 Replies
Jul 26, 2011
i have a treeview with 8 Root items, my program needs to add items to the treeview without me selecting the proper root or item, the root items have been coded so they cannot be deleted, but i'm not sure how to get the software to place data in a particular root by what its name is. when you add children the indexs change so im not sure where to begin. heres an example
[Code]...
View 3 Replies
May 14, 2012
how whould I go about deleteing an item from a listbox and it will select the next available item.you know like the treeview control selects the next node if you delete one.
View 2 Replies
Jan 27, 2011
We are doing a query against an in-memory collection of LINQ data objects. The wrinkle is that we are ordering by a column in a related table whose records have not necessarily been loaded yet (deferred loading:)
Dim oPkgProducts = _
From b In oBillPkg.BillProducts _
Where b.Successful.GetValueOrDefault(Common.X_INDETERMINATE) = _
[code]....
View 1 Replies
Mar 28, 2012
I have a Customer object which has a collection of ContactNumbers. Is it possible with LINQ to get a list of Customers where one of the contact numbers = '123'?
Public Class Customer
Public Overridable Property ContactNumbers As List(Of ContactNumber)
End Class
[Code].....
View 1 Replies
Dec 16, 2009
Nearly identical to How can I get every nth item from a List<T>?But I'm having trouble turning
List<T> list = originalList.Where((t,i) => (i % 5) == 0).ToList();
Into VB.Net code.
View 1 Replies
Oct 8, 2009
I'm building flat file content from collections of strings.
Example collection: A, B, C, D, E, etc.
I want to be able to output these values to a string with line feeds in one swoop with Linq if possible.[cod]e...
View 8 Replies
Mar 6, 2012
How to get the last instance of label control in List (of Control) using linq?
View 4 Replies
Jun 29, 2009
Private Function GetWebDataGridOKButtonId() As String
Dim ctls As ControlCollection = _
WebDataGrid1.Controls(0).Controls(0).Controls
[Code]....
This is not working for me. I am trying to iterate a control collection and return one control ID.
View 2 Replies
May 17, 2011
I have created collection and adding values in the collection, later I need to find if uer passed value is exist in the collection or not, I use contains finction to find the answer but it always returns false even though user passed value exist in the collection list. here is my code:
[Code]...
View 3 Replies
Aug 17, 2009
This is my code.Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
[Code]...
View 6 Replies
Dec 26, 2010
I want to do something different on the last KeyValuePair of the Dictionary I'm iterating on.
For Each item In collection
If ItsTheLastItem
DoX()
[code].....
View 7 Replies
Oct 14, 2009
Is there a way to remove 'this' item from a collection, i.e the one I'm working with, or do I always need an index?
For each ThisItem in MyCollection
if a=true then MyCollection.Remove(ThisItem)
next
[code].....
View 13 Replies
Jan 30, 2011
I'm building a WP7 (VB.NET) app and have a custom class, PinInfo, of which I have two ObservableCollections, TempPins and FavoritePins.
In various parts of the app, I need a unique identifier to get a reference to a particular PinInfo in the collection, so at present, I assign an ID to the PinInfo when it's added to the collection, then later get a reference to the pin via the ID.
For example, if I create a button to delete a pin, I set its tag to the pin's ID, then the button click handler reads the button's tag to get the corresponding pin from the collection (via LINQ).
I generate the ID by adding one to the last-assigned ID, but these lists are often modified (i.e. items removed), so I end up with needlessly large ID numbers (e.g. just a few items, with IDs in the thousands).
View 1 Replies
Jun 7, 2012
On my view there is a datatable holding a collection of ServerRow and 4 textboxes (Hostname, OS, Location, Zone). When the user types into any of the 4 boxes I want to immediately filter this list. I have this code setup and ready to go that on the set method of each textbox property I call the filter method.
Now onto the problem: if I have 4 criteria that means I have 2 to the power of 4 different scenarios. What I would like to do is write a linq statement with all of these scenarios dealt with such that if any of the properties are NullOrEmpty they will not be used in the Where clause and on the flip side if there is a value the corresponding field will be searched with a Contains.
Example
AvailableCis = New ObservableCollection(Of ServerRow)
(_CiData.Where(Function(ci) ci.OS.ToUpper
.Equals(_selectedOS.ToUpper) AndAlso
ci.HostName.ToUpper.Contains(_ciNameFilterText.ToUpper))
.OrderBy(Function(a) a.CiName))
This example shows how it works with 2 criteria and both of those criteria are set.
View 1 Replies