所以我有這個疑問:如何設置默認爲空查詢
var list = (from x in xList
join a in AList on x.commonfield equals a.commonfield
join b in BList on x.newCommonField equals b.newCommonField
from c in Clist.Where(p=>p.connectorID==x.connectorID).DefaultifEmpty()
select new { x, a.DesiredFieldFromA, b.DesiredFieldFromB,c.name}).ToList();
list.ForEach(el =>
{
el.x.DesiredFieldFromA= el.DesiredFieldFromA;
el.x.DesiredFieldFromB= el.DesiredFieldFromB ;
el.x.Name=el.name;
});
return list.Select(p=>p.x);
當我這樣做:
select new { x, a.DesiredFieldFromA, b.DesiredFieldFromB,c.name}).ToList();
問題1:我怎麼能告訴它應該仍然顯示我的記錄...但如果名稱字段爲空,則放入string.empty。
沿東西:
select new { x, a.DesiredFieldFromA, b.DesiredFieldFromB,c.name??string.Empty}).ToList();
問題2:我怎麼能告訴它,即使「連接器ID」爲空..我還需要它來顯示X提前錄製
謝謝!
解決方案#1和#2如下:
from c in Clist.Where(p=>p.connectorID==x.connectorID).DefaultifEmpty()
select new { x, a.DesiredFieldFromA, b.DesiredFieldFromB,name= (c==null) ?"Not Defined": c.name}).ToList();
對於問題2,你在找一個'LEFT OUTER JOIN'類似的東西? – bluevector
是的......但是使用它給了我很多重複的connectorID = null的記錄。我正在爲修復atm工作。 :)謝謝 –