Reputation: 3137
I know that to return an anonymous type I can do this:
var test = from c in list
where c.SomeField > 200
select new { c.SomeField, c.AnotherField };
And that's ok.
Now, I'd like to now how to return this anonymous type doing the same thing but using the Where method. Something like:
var test = list.Where((SomeType c) => { if (c.SomeField > 200)
return new { c.SomeField, c.AnotherField } });
Is there any way of doing it using the Where method?
Thank you!
Upvotes: 1
Views: 86
Reputation: 101681
Where
is not designed for this. Where
expects a delegate that takes an object and returns a bool
. So, returning an anonymous type in Where
is not possible and doesn't make sense.
You can only filter your items with Where
and then do the projection with Select
var test = list.Where(c=> c.SomeField > 200)
.Select(c => new { c.SomeField, c.AnotherField });
Btw this is equivelant of your first query since it will be translated into this.
Alternatively, if you are working with List<T>
, you can use List<T>.FindAll
method to get all the items that matches with your predicate.
Upvotes: 7