m4rc
m4rc

Reputation: 3006

Joining two separate linq queries

I have a LINQ query which results an array of Job records. Due to LINQ-To-SQLs limitations/restrictions regarding cross-context queries, I can't do a join a table and so two of the fields for the Job record are empty.

I can however get these by doing a separate LINQ query.

My question is can I populate those two fields easily say for example by doing a join on those two queries? - and if so how?

query1.Join(query2,..... and so on);

Thanks in advance.

EDIT

var results = query1.Join(query2,
                        job => job.JobID,
                        other => other.JobID,
                        (job, other) => new
                        {
                            MissingField = other.Field,
                            OtherMissingField = other.OtherField
                        });

I am getting the error message: The type arguments for method 'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func, System.Func, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Upvotes: 3

Views: 2994

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062550

If the data is sized such that you can bring it all into memory, then you can do the join in LINQ-to-Objects; just add some .ToList() or .AsEnumerable() to the original queries - the LINQ to join them remains the same.

However; this cannot be used to join at the server. For that, either throw the tables you need into a single data-context, or cheat and use TSQL for that query. LINQ-to-SQL's ExecuteQuery<T> method is pretty handy for such purposes.

Upvotes: 2

Related Questions