I have managed to get the following working:
var transactions = from t in context.Transactions
group t.Create_Date_Time by t.Participation_Id
into t1
select new { ParticipationId = t1.Key, CreateDateTime = t1.Max() };
var cases = from c in context.Cases
group c.Create_Date_Time by c.Participation_Id
into c1
select new { ParticipationId = c1.Key, CreateDateTime = c1.Max() };
var interactions = from i in context.Interactions
join pp in context.Party_Participation on i.Party_Id equals pp.Party_Id
group i.Last_Update_Date_Time.HasValue ? i.Last_Update_Date_Time : i.Create_Date_Time
by pp.Participation_Id
into i1
select new { ParticipationId = i1.Key, CreateDateTime = i1.Max() };
transactions.Union(cases);
However at last when I was trying to add third output,
transactions.Union(interactions);
// or
interactions.Union(transactions);
I got the following error
either way it is throwing following error
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Linq.IQueryable<AnonymousType#2>'
Error 2 'System.Collections.Generic.List<AnonymousType#1>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments
The only difference in third sequence is, I am using join with another table. I have tried .AsEnumerable()
, .ToList()
and .ToArray()
, none of them helped.
When you create interactions
, its type is not an anonymous type of int and DateTime, it is of int and nullable DateTime. This is because in your inline if statement, you never call .Value
off of the nullable column. Your code should work if you create interactions
like this:
var interactions = from i in context.Interactions
join pp in context.Party_Participation on i.Party_Id equals pp.Party_Id
group i.Last_Update_Date_Time.HasValue ? i.Last_Update_Date_Time.Value : i.Create_Date_Time by
pp.Participation_Id
into i1
select new {ParticipationId = i1.Key, CreateDateTime = i1.Max()};
Simpler Example:
var lst = new int?[] { 2,3,null,5,5 };
var lst2 = new int[] { 2,3,4,5,6 };
lst.Select(x => x.HasValue ? x.Value : 0).Union(lst2); //works fine
lst.Select(x => x.HasValue ? x : 0).Union(lst2); //throws error
lst.Select(x => x ?? 0).Union(lst2); //also works
Even though we can easily see that the inline if statement will never return a null value in either case, the compiler cannot make such guarantees and has to type the return value to that of a nullable int in the second case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With