[英]LINQ Union between two tables with the same fields and then returned in a collection
I have given up trying to create a linq query to retrieve a sql server view which is a union between two tables. 我已经放弃尝试创建一个linq查询来检索一个sql server视图,它是两个表之间的联合。 I will now try to create a linq union. 我现在将尝试创建一个linq联盟。
I have two views, MemberDuesPaid and MemberDuesOwed. 我有两个视图,MemberDuesPaid和MemberDuesOwed。 They have the same fields in both; 他们两个都有相同的领域; (BatchNo, TranDate, DebitAmount, CreditAmount, ReceiptNo, CheckNo, SocSecNo). (BatchNo,TranDate,DebitAmount,CreditAmount,ReceiptNo,CheckNo,SocSecNo)。
I also have a helper class in my application which is called MemberTransaction. 我的应用程序中还有一个名为MemberTransaction的帮助器类。 It has all the same properties. 它具有所有相同的属性。
How how do i do a union between the two tables where socSecNo = the ssn passed in? 如何在两个表之间建立联合,其中socSecNo = ssn传入? I want to union the two tables and return an IEnumerable collection of MemberTransaction. 我想结合两个表并返回一个IEnumerable MemberTransaction集合。 After the two tables are unioned together i want to have the collection that is returned ordered by trandate in descending order. 在将两个表联合在一起后,我希望按降序顺序返回由trandate排序的集合。
You can do it in a Linq Union query: 您可以在Linq Union查询中执行此操作:
var infoQuery =
(from paid in db.MemberDuesPaid
select new MemberTransaction() {
BatchNo = paid.BatchNo,
TranDate = paid.TranDate,
DebitAmount = paid.DebitAmount,
CreditAmount = paid.CreditAmount,
ReceiptNo = paid.ReceiptNo,
CheckNo = paid.CheckNo,
SocSecNo = paid.SocSecNo})
.Union
(from owed in db.MemberDuesOwed
select new MemberTransaction() {
BatchNo = owed.BatchNo,
TranDate = owed.TranDate,
DebitAmount = owed.DebitAmount,
CreditAmount = owed.CreditAmount,
ReceiptNo = owed.ReceiptNo,
CheckNo = owed.CheckNo,
SocSecNo = owed.SocSecNo});
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): 如果你想要不同的值,你可以在上面的语句之后做这样的事情(如果你把所有内容括起来,你可以内联,但这更容易解释):
infoQuery = infoQuery.Distinct();
The variable infoQuery
will by this time be populated entirely with objects of type MemberTransaction
rather than the two disparate types in the union statement. 此时变量infoQuery
将完全填充MemberTransaction
类型的对象,而不是union语句中的两个不同类型。
Assuming you've got two collections, one representing each view: 假设你有两个集合,一个代表每个视图:
var paid = new List<MemberDuesPaid>();
var owed = new List<MemberDuesOwed>();
Convert both collections above to instances of the third class before performing the union: 在执行联合之前,将上面的两个集合转换为第三个类的实例:
var everyone
= paid.Select(x => new MemberTransaction { BatchNo = x.BatchNo, ... })
.Union(owed.Select(x => new MemberTransaction { BatchNo = x.BatchNo, ... }))
.Where(x => x.SocSecNo == ssn)
.OrderByDescending(x => x.TranDate)
.ToList();
Now you've got a collection of MemberTransaction
, but there's nothing to indicate how one MemberTransaction
equals another. 现在你已经有了MemberTransaction
的集合,但是没有任何东西可以表明一个MemberTransaction
如何等于另一个。 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 the MemberTransaction
class. 你必须通过在MemberTransaction
类上实现IEquatable<T>
来告诉它是什么让两个实例相等。
public class MemberTransaction : IEquatable<MemberTransaction>
{
public int BatchNo { get; set; }
public DateTime TranDate { get; set; }
public decimal DebitAmount { get; set; }
public decimal CreditAmount { get; set; }
public int ReceiptNo { get; set; }
public int CheckNo { get; set; }
public int SocSecNo { get; set; }
public bool Equals(MemberTransaction other)
{
return BatchNo == other.BatchNo
&& TranDate.Equals(other.TranDate)
&& DebitAmount == other.DebitAmount
&& CreditAmount == other.CreditAmount
&& ReceiptNo == other.ReceiptNo
&& CheckNo == other.CheckNo
&& SocSecNo == other.SocSecNo;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.