1

所以我有这个查询:

   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:我怎么能告诉它它仍然应该向我显示记录......但是如果 name 字段为空,则放置 string.empty。
一些东西:

select new { x, a.DesiredFieldFromA, b.DesiredFieldFromB,c.name??string.Empty}).ToList();

问题#2:我怎么能告诉它即使“connectorID”为空..我仍然需要它来显示 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();
4

2 回答 2

1

对于问题 #2,更改:

from c in Clist.Where(p=>p.connectorID==x.connectorID).DefaultidEmpty()

至:

from c in Clist.Where(p=>p.connectorID==x.connectorID || x.connectorID == null)
于 2012-06-21T13:07:49.350 回答
0

对于问题 #1,试试这个:

select new { 
    x, 
    a.DesiredFieldFromA, 
    b.DesiredFieldFromB, 
    name = c.name ?? string.Empty
}).ToList();

您需要指定名称“名称”,以便您可以使用您的表达式来分配它。

对于问题 #2,请参阅jonnyGold 的回答

于 2012-06-21T13:04:52.470 回答