在EF上使用Union时如何选择所有记录

时间:2019-03-27 02:13:13

标签: c# entity-framework

我正在尝试汇总来自不同表的所有交易。当我在所有表结果之间使用联合时,它在查询中添加了不同的内容,并且事务摘要未显示所有详细信息。

var InTemp =    from a in db.InDetail
                select new Models.TransactionList
                {
                    InOutType = "In",
                    OrderDate = a.OrderDate,
                    Total = a.Total,
                    Operator = a.User,
                    OperationDateTime = a.DateTime
                };
var OutTemp = from a in db.OutDetail
                select new Models.TransactionList
                {
                    InOutType = "Out",
                    OrderDate = a.OrderDate,
                    Total = a.Total,
                    Operator = a.User,
                    OperationDateTime = a.DateTime
                };    
var ReleaseTemp = from a in db.ReleaseDetail
                    select new Models.TransactionList
                    {
                        InOutType = "Release",
                        OrderDate = a.OrderDate,
                        Total = a.Total,
                        Operator = a.User,
                        OperationDateTime = a.DateTime
                    };                        
var query = InTemp.Union(OutTemp).Union(ReleaseTemp);

上面的代码在添加Union的同时,EF添加Distinct,并且结果仅显示唯一的记录,相反,我需要基于选择的所有记录。谢谢

1 个答案:

答案 0 :(得分:2)

使用Concat()代替Union()

var query = InTemp.Concat(OutTemp).Concat(ReleaseTemp);

Union()的行为与Sql union相同,并返回不同的结果,而Concat()与Sql Union all的行为相同。

Live Demo