处理链接 linq-to-sql 查询表达式中的空值

发布于 2024-10-13 00:12:25 字数 1710 浏览 5 评论 0原文

我有一个 L2S 存储库查询,我正在努力以一种很好的方式编写它。它看起来像......

_orderRepository
    .GetAllByFilter(o => o.CustomerId == id)
    .Select(o => 
         new CustomerOrderRecord
         (
              o.Id,
              o.PartNumber,
              o.Date
              // ... etc, more order details

             /* Here I need the last DateTime? the customer placed
                an order for this item, which might be null.
                So I end up with the following horrible part of
                the query */  
              o.Customer.CustomerOrderRecords
                    .Where(x => x.PartNumber == o.PartNumber)
                    .OrderByDescending(x => x.Date).FirstOrDefault()
                    == null ? null : 
                          o.Customer.CustomerOrderRecords
                             .Where(x => x.PartNumber == o.PartNumber)
                             .OrderByDescending(x => x.Date).First().Date;

         )).ToList();

所以希望您能看到我必须将整个查询链编写两次的问题,只是为了在接收 LastOrdered 值时进行空检查。

这需要内嵌编写(我认为),因为 GetAllByFilter 返回一个 IQueryable

我尝试在 select 语句中使用中间变量,因此我得到了类似于以下内容的内容,但我无法编译类似的内容。

.Select(o => 
         new CustomerOrderRecord
         (
              o.Id,
              o.PartNumber,
              o.Date
              // ... etc, more order details

              var last = o.Customer.CustomerOrderRecords
                    .Where(x => x.PartNumber == o.PartNumber)
                    .OrderByDescending(x => x.Date).FirstOrDefault()
                    == null ? null : last.Date;

          )).ToList();

有没有可用的语法技巧来解决这个问题?

I have a L2S repository query which I'm stuggling to write in a nice way. It looks something like...

_orderRepository
    .GetAllByFilter(o => o.CustomerId == id)
    .Select(o => 
         new CustomerOrderRecord
         (
              o.Id,
              o.PartNumber,
              o.Date
              // ... etc, more order details

             /* Here I need the last DateTime? the customer placed
                an order for this item, which might be null.
                So I end up with the following horrible part of
                the query */  
              o.Customer.CustomerOrderRecords
                    .Where(x => x.PartNumber == o.PartNumber)
                    .OrderByDescending(x => x.Date).FirstOrDefault()
                    == null ? null : 
                          o.Customer.CustomerOrderRecords
                             .Where(x => x.PartNumber == o.PartNumber)
                             .OrderByDescending(x => x.Date).First().Date;

         )).ToList();

So hopefully you can see the problem that I'm having to write the whole query chain twice just to do the null check when receiving the LastOrdered value.

This needs to be written in-line (I think) because GetAllByFilter returns an IQueryable.

I tried to use an intermediate variable within the select statement, so I'd have something a bit like the following, but I couldn't get anything like that to compile.

.Select(o => 
         new CustomerOrderRecord
         (
              o.Id,
              o.PartNumber,
              o.Date
              // ... etc, more order details

              var last = o.Customer.CustomerOrderRecords
                    .Where(x => x.PartNumber == o.PartNumber)
                    .OrderByDescending(x => x.Date).FirstOrDefault()
                    == null ? null : last.Date;

          )).ToList();

Is there a syntax trick available which solves this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-10-20 00:12:25

尝试使用 Select 获取 Date 成员:

o.Customer.CustomerOrderRecords
    .Where(x => x.PartNumber == o.PartNumber)
    .OrderByDescending(x => x.Date)
    .Select(x => (DateTime?)x.Date)
    .FirstOrDefault()

Try using Select to fetch the Date member:

o.Customer.CustomerOrderRecords
    .Where(x => x.PartNumber == o.PartNumber)
    .OrderByDescending(x => x.Date)
    .Select(x => (DateTime?)x.Date)
    .FirstOrDefault()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文