I have a list of foo called crepes. I want to return foo where bar.doritos == "coolRanch"
class foo
{
List<bar> item;
string candy;
string beer;
}
class bar
{
string doritos;
string usb;
}
var item = crepes.item.Where(x => x.doritos == "coolRanch").FirstOrDefault();
From other threads, i've pieced together the above linq query, but crepes.item throws an error. "List does not contain a definition for 'item' and no definition for 'item' accepting first argument...
How to query an ArrayList with LINQ (C#) This example uses LINQ to query over an ArrayList in C#. You must declare the type of the range variable to reflect the type of the objects in the collection.
You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API.
In LINQ, a query variable is any variable that stores a query instead of the results of a query. More specifically, a query variable is always an enumerable type that will produce a sequence of elements when it is iterated over in a foreach statement or a direct call to its IEnumerator.
Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source.
Given that crepes is a List<Foo>
, you need to add an additional level to the linq query.
var item = crepes.Where(a => a.item.Any(x => x.doritos == "coolRanch")).FirstOrDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With