简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM