更有效的Linq表达

时间:2017-08-31 14:03:35

标签: c# linq

我有这个Linq加入

var NewQuote = (from qw in Q
                        join NW in NewNotes on qw.RECID equals NW.RECID into temp
                        from j in temp
                        select new Quotes
                        {
                            QuoteNumber = qw.QuoteNumber,
                            CustPartNumber = qw.CustPartNumber,
                            ITEMGROUPID = qw.ITEMGROUPID,
                          LotSize = qw.LotSize,
                          EAU = qw.EAU,
                          CONTACTPERSONID = qw.CONTACTPERSONID,
                          QUOTATIONSTATUS = qw.QUOTATIONSTATUS,
                          QUOTESENTDATE = qw.QUOTESENTDATE,
                          PricePerPiece = qw.PricePerPiece,
                          QuoteValue = qw.QuoteValue,
                          Email = qw.Email,
                          RECID = qw.RECID,
                          Notes = j == null ? "" : j.NOTES
                        }).ToList();

Q是Quote类,但我需要将数据添加到NewNotes的Notes字段中。有没有比列出Quote类中的每个字段更好的方法呢?如果我必须在Quote中添加字段,那么我必须记录这段代码并进行更新。

1 个答案:

答案 0 :(得分:2)

如果您只想更新一个属性,为什么要创建Quotes的新实例?

var query = from qw in Q join NW in NewNotes 
            on qw.RECID equals NW.RECID into temp 
            from j in temp
            select new { Quote = qw, Notes = j?.Notes ?? "" };

foreach(var x in query)
{
    x.Quote.Notes = x.Notes;
}