2017-08-31 49 views
0

我有这样的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是引用类的,但我需要将数据从NewNotes添加备注字段。有没有更好的方法来做这件事,而不是从Quote类中列出每个字段?如果我必须添加字段到Quote,那么我必须记录下来这段代码并更新。

+2

“高效”是指“少打字”,对吗? –

+0

是的。除此之外,这很容易成为隐藏的“陷阱”。看看是否有更好的方法。 – Anthony

+1

让这个更“高效”的一种方法是开始使用命名约定 –

回答

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; 
}