繁体   English   中英

在LINQ中使用IF类型语句对实体SELECT NEW子句C#

[英]using a IF type statement in a linq to entities SELECT NEW clause C#

我从2个表中返回数据,一个表包含停止信息,另一个表包含传递信息。 可能有一个停顿而没有交付,或者有多次交付。

我要返回一个数据网格,我想要一列显示记录是停止还是停止交付。

如果记录没有交付(为空),则当前查询在deliveryID字段中返回“ 0”。

我想添加另一个名为rowType的字段,其中,如果deliveryID = 0,它将返回“仅停止”的值,如果返回“停止并交付”,则以下代码将正常工作,但没有新字段。

  var combinedEvents = (from d in dbContext.stop_details
                              join s in dbContext.stop_event on d.id equals s.stop_id into Inners
                              from sd in Inners.DefaultIfEmpty()
                              where (d.ship_date >= startDate && d.ship_date <= endDate)
                              select new
                              {
                                  deliveryID = (int?)sd.id ?? 0,
                                  stopID = d.id,
                                  d.ship_date,
                                  d.cust_ref_1_BOL,
                                  d.cust_ref_2_OrderNum,
                                  sd.received_by
                                  }).ToList();

我可以这样做吗?

使用条件? 运算符,然后复制您的逻辑deliveryID

rowType = ((int?)sd.id).HasValue ? "Stop With Delivery" : "Stop Only"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM