Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 291
m4rc Avatar asked Mar 26 '26 03:03

m4rc


1 Answers

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.

like image 131
Marc Gravell Avatar answered Mar 27 '26 17:03

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!