0

I have formed the following LINQ query, but .. . . .

if you have a third join ...in this case condional... how write this ?

example :

 var query = from trip in db.Trips from driver in db.Drivers condition1 ? join X in other_table : join Y in other_table2 where trip.DriverId == driver.DriverId || trip.CoDriverId == driver.DriverId select new { driver.DriverId, trip.TripId }; 
3
  • did you consider constructing 2 different queries based on condition, as oppose to trying to dynamically assembling it Commented Dec 11, 2013 at 17:48
  • I recommend create an appropriate view at database level and query it via LINQ. Commented Dec 11, 2013 at 17:50
  • you can do left join on all other tables and then operate with them Commented Dec 11, 2013 at 17:52

2 Answers 2

2

If the type of other_table and other_table2 where the same, then the easiest way to do this would be:

IQueryable<SomeType> joinTable = condition1 ? other_table : other_table2; 

And then use join j in joinTable as part of your query.

Otherwise, if you are only using the join to restrict a where condition, rather than to affect the fields, you could first write query to ignore that and then add something like:

if(condition1) query = query.Where(q => other_table.Where(o => foo==bar).Select(o => o.DriverId).Contains(q.DriverId)); else query = query.Where(q => other_table.Select(o => o.DriverId).Contains(q.DriverId)); 

Here the restriction of foo==bar is to show that we can add quite a bit of detail to these further Where clauses if necessary. The main thing is that Where doesn't change the type of the result; query is IQueryable<T> for some T, and remains so after the Where so we can assign that query back to query.

If the join is to add values then:

var newQuery = condition1 ? query.Join(other_table, q=>DriverId, ot => ot.DriverId, (q, ot) => new {q.DriverID, q.TripId, ot.OtherField} : query.Join(other_table2, q=>DriverId, ot => ot.DriverId, (q, ot) => new {q.DriverId, q.TripId, OtherField = ot.YetAnotherField}; 

Note that other_table.OtherField and ot.YetAnotherField must be of the same type. This depends on the compiler creating any anonymous classes with the same name, order and type of fields as the same class, so newQuery's type can be deduced (and is the same for each branch). If they are not the same type then you need to either cast one to the type of the other, or else do something like:

var newQuery = condition1 ? query.Join(other_table, q=>DriverId, ot => ot.DriverId, (q, ot) => new {q.DriverID, q.TripId, ot.OtherField, YetAnotherField = (string)null} : query.Join(other_table2, q=>DriverId, ot => ot.DriverId, (q, ot) => new {q.DriverId, q.TripId, OtherField = -1, ot.YetAnotherField}; 

In this last example, OtherField is an integer which is set to -1 if condition1 was true, while YetAnotherField is a string which is set to null if condition1 was false.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understood the core question, so:

 var query = from trip in db.Trips from driver in (condition1 ? cdb.Drivers1 : cdb.Drivers2) where trip.DriverId == driver.DriverId || trip.CoDriverId == driver.DriverId select new { driver.DriverId, trip.TripId }; 

2 Comments

It'll work if the two sources are of the same type, not otherwise
It's true (it also seems that he uses TypedDataSet then certainly it is different types). Your answer is correct. Another suggestion: Sometimes you should concatenate the tables at the database level (in DataAdapter), with the UNION.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.