Union in LINQ
-
What is the criteria for a union in Linq. The criteria for SQL is that the select clumns must have the same name & amount of columns between the two tables. This is what I have & an error is thrown in linq. var rsPunch = (from a in PunchCtx.AccessPunches select new { a.1, a.2 }); var rsPunch1 = (from t in PunchCtx.TimeAtendancePunches select new { t.1, t.2 }); var union = rsPunch.Union(rsPunch1);
-
What is the criteria for a union in Linq. The criteria for SQL is that the select clumns must have the same name & amount of columns between the two tables. This is what I have & an error is thrown in linq. var rsPunch = (from a in PunchCtx.AccessPunches select new { a.1, a.2 }); var rsPunch1 = (from t in PunchCtx.TimeAtendancePunches select new { t.1, t.2 }); var union = rsPunch.Union(rsPunch1);
LINQ operates in a similar way to SQL but not exactly the same. To answer this try the following statements before the union:
var t1 = rsPunch.GetType().ToString();
var t2 = rsPunch1.GetType().ToString();You should get something like this:
t1 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType0`2[..data types..]]"
t2 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType1`2[..data types..]]"I hope you can see that the two queries acutally generate data queries of *different types*. The 'new {}' statement creates an anonymous type which is different each time, even though the types of the elements inside are the same.
'Howard
-
LINQ operates in a similar way to SQL but not exactly the same. To answer this try the following statements before the union:
var t1 = rsPunch.GetType().ToString();
var t2 = rsPunch1.GetType().ToString();You should get something like this:
t1 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType0`2[..data types..]]"
t2 = "System.Data.Linq.DataQuery`1[<>f__AnonymousType1`2[..data types..]]"I hope you can see that the two queries acutally generate data queries of *different types*. The 'new {}' statement creates an anonymous type which is different each time, even though the types of the elements inside are the same.
'Howard
Oops I deleted the "how to fix bit". To union the results, ensure they create the same type of value. Perhaps create a struct or class than contains the two values and a constructor. Or you might be able to use a KeyValuePair<TKey, TValue> if that is easier. Then you should be able to use Union.
'Howard