Search code examples
c#linq

How to select all properties except for a specific property?


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.


Solution

  • 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?