是否可以将此表达式转换为LINQ?

TermsOfPayment termsOfPayment = null;
foreach (CustomerGroup group in _customer.CustomerGroups)
    if (termsOfPayment == null) termsOfPayment = group.TermsOfPayment;
    else if (group.TermsOfPayment != null)
        if (group.TermsOfPayment.InvoiceDueDays < termsOfPayment.InvoiceDueDays)
            termsOfPayment = group.TermsOfPayment;


自从上面的表达式解决了这个问题以来,这似乎是一个愚蠢的问题,但是我使用了一些LINQ表达式,并且渴望了解更多-因此,发布本文的原因。

基本上,我只想从客户所属的组中选择具有最小InvoiceDueDays(整数)值的TermsOfPayment对象。

最佳答案

termsOfPayment = (
                   from g in _customer.CustomerGroups
                   where g.TermsOfPayment != null
                   orderby g.TermsOfPayment.InvoiceDueDays
                   select g.TermsOfPayment
                 ).FirstOrDefault();

10-01 23:36