Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Linq, what is the opposite of .Select()?

Tags:

c#

linq

In a Linq query, if I'd want to select all properties except for a specific property, what would I do?

I can't use Select() and specify all properties except the one I don't want, because I don't know some of the properties (I query a list of abstract class).

I can't also just select all properties because that'd throw a circular reference was detected while serializing an object of type X. (I'm serializing the object to Json)

Is there any Filter() method or some extension method I can use?

Thanks.

like image 527
Alon Gubkin Avatar asked Jun 25 '11 13:06

Alon Gubkin


People also ask

What is any () in LINQ?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.

What is LINQ select?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.

What does LINQ Select return?

By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.

How does LINQ select work?

Select is used to project individual element from List, in your case each customer from customerList . As Customer class contains property called Salary of type long, Select predicate will create new form of object which will contain only value of Salary property from Customer class.


1 Answers

No, you can't do that - there's nothing like that at all. Bear in mind that you have to end up with a particular type as a result of the projection... if you don't know what properties you're going to select, how can you have such a type?

If you're querying a list of some abstract class, is there any reason you don't want to just keep a reference to the instance of that abstract class? What benefit is there in separating it out into specific properties? Or are you really trying to avoid seeing those properties later on, e.g. for databinding?

like image 76
Jon Skeet Avatar answered Nov 10 '22 18:11

Jon Skeet