I have given up trying to create a linq query to retrieve a sql server view which is a union between two tables. I will now try to create a linq union.
I have two views, MemberDuesPaid and MemberDuesOwed. They have the same fields in both; (BatchNo, TranDate, DebitAmount, CreditAmount, ReceiptNo, CheckNo, SocSecNo).
I also have a helper class in my application which is called MemberTransaction. It has all the same properties.
How how do i do a union between the two tables where socSecNo = the ssn passed in? I want to union the two tables and return an IEnumerable collection of MemberTransaction. After the two tables are unioned together i want to have the collection that is returned ordered by trandate in descending order.
You can do it in a Linq Union query:
That should return you a set with everything combined into a single list.
[Edit]
If you want distinct values, you can do something like this after the above statement (you can do it inline if you bracket everything, but this is simpler to explain):
The variable
infoQuery
will by this time be populated entirely with objects of typeMemberTransaction
rather than the two disparate types in the union statement.Assuming you've got two collections, one representing each view:
Convert both collections above to instances of the third class before performing the union:
Now you've got a collection of
MemberTransaction
, but there's nothing to indicate how oneMemberTransaction
equals another. So if you just run the above, you'll end up with everything from both collections in the result, instead of a true union.You have to tell it what makes two instance equal, by implementing
IEquatable<T>
on theMemberTransaction
class.