我有一个Linq
代码如下:
var checkList = (from dc in _debtorCreditorService.GetAllDebtorCreditors()
join n in _notificationService.GetAllNotification()
on dc.MaturityDate equals n.CreatedDate
where n.CreatedDate.Day == today.Day
select new
{
chek_id = n.TypeId,
date = n.CreatedDate,
title = n.Title
})
.Distinct()
.ToList();
我有一个如下的订单:
List<Order> order
this.order属性为orderId
和orderDate
我想将这两个列表合并到一个列表中以获得{check_id,date,title,OrderId and OrderDate}
并通过JSON发送该列表以进行查看
答案 0 :(得分:2)
我想将
Union
用于checkList和List<Order>
,但我不知道 怎么样。结果应为List<anonymous tpe>
嗯,那实际上很容易:
var orders = orderList.Select(o => new
{
chek_id = o.TypeId,
date = o.CreatedDate,
title = o.Title
});
var resultList = checkList.Union(orders).ToList();
这假设Order
中存在这些属性,但我确信你明白了。
如果您只需要第一个查询来“提供”联盟,那么可以提高效率,Distinct
和ToList
是不必要的,因为Union
会删除重复项。
答案 1 :(得分:2)
For Making Union两个侧列名称必须相同。
// First Collect CheckList
var checkList = (from dc in _debtorCreditorService.GetAllDebtorCreditors()
join n in _notificationService.GetAllNotification()
on dc.MaturityDate equals n.CreatedDate
where n.CreatedDate.Day == today.Day
select new Order
{
chek_id = n.TypeId,
date = n.CreatedDate,
title = n.Title ,
OrderId=0,
OrderData=null
})
.Distinct()
.ToList();
// Secondly ,Collect Orders
var lstOrder=(from n in Order
select new Order
{
chek_id = n.TypeId,
date = n.CreatedDate,
title = n.Title ,
OrderId=n.OrderId,
OrderData=n.OrderDate
});
// Finally ,Make Union of both the Table
var result=checkList.Union(lstOrder).Tolist();