Linq linq-to-entities add join if condition is met?

You can conditionally add to your query, but you need to understand that once your query is typed, you cannot change its type to something else. Your problem is that one of your queries types to an anonymous type, and another types to an entity, and that is inconsistent For an example that works, see: var query = context.Foos.AsQueryable(); if (DateTime.Now. Second % 2 == 0) { query = query.

Join(context. Bars, f => f. BarId, be => b.Id, (f, b) => new { f, be }).

Select(item => item. F); } else { query = query. OrderByDescending(f => f.

Id); } The reason this works is that the initial declaration is an IQueryable, then finally pull out your anonymous type. As long as your type is the same regardless of the other logic you are including, you will be able to build upon your single query However, if the logic affects the shape of the data you actually retrieve, then that's where you're out of luck, you will need go another direction.

You can conditionally add to your query, but you need to understand that once your query is typed, you cannot change its type to something else. Your problem is that one of your queries types to an anonymous type, and another types to an entity, and that is inconsistent. For an example that works, see: var query = context.Foos.AsQueryable(); if (DateTime.Now.

Second % 2 == 0) { query = query. Join(context. Bars, f => f.

BarId, be => b. Id, (f, b) => new { f, be }). Select(item => item.

F); } else { query = query. OrderByDescending(f => f. Id); } The reason this works is that the initial declaration is an IQueryable, and each of the resulting queries (either the join or the ordered version) is still an IQueryable.

If you need to project your query result to anonymous type, I recommend saving that for a later step (a query of your constructed query). Construct all of your joins, your filtering, your orders, etc. , then finally pull out your anonymous type.As long as your type is the same regardless of the other logic you are including, you will be able to build upon your single query. However, if the logic affects the shape of the data you actually retrieve, then that's where you're out of luck, you will need go another direction.

1: Throwing my weight behind this superior answer. – StriplingWarrior Jul 1 at 22:03 thanks for this answer, sheds quit sime light on how it works to me! – Seb Jul 2 at 9:04.

That's because the result of the Join() is a type, that is different from the type of the original query. How should the code after the condition treat the variable? It could contain one of two different types.So, doing it like this is simply not possible.

You can conditionally add to your query, but you need to understand that once your query is typed, you cannot change its type to something else. Your problem is that one of your queries types to an anonymous type, and another types to an entity, and that is inconsistent. The reason this works is that the initial declaration is an IQueryable, and each of the resulting queries (either the join or the ordered version) is still an IQueryable.

If you need to project your query result to anonymous type, I recommend saving that for a later step (a query of your constructed query). Construct all of your joins, your filtering, your orders, etc., then finally pull out your anonymous type. As long as your type is the same regardless of the other logic you are including, you will be able to build upon your single query.

However, if the logic affects the shape of the data you actually retrieve, then that's where you're out of luck, you will need go another direction.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions