可选择在linq中加载/选择列

时间:2014-01-14 17:26:45

标签: c# linq

所以我想说我有一个像这样的linq查询

var stuff = from t1 in TableOne
            let t2 = t1.TableTwo
            where t1.id = "someId"
            select
            new
               {
                 column1 = t1.Col1,
                 column2 = t2.Col1
                 column3 = (from t3 in TableThree
                            where t3.id = "someId"
                            select new SomeObject
                            {
                                Field1 = t3.Col1,
                                Field2 = t3.Col2
                            }).ToList()
                }

现在在某些情况下我想在第3列中加载数据,就像传入方法的参数包含的那样,这不是null。当参数为null时,我不想为了效率而加载它。关于如何达到预期效果的任何建议?

1 个答案:

答案 0 :(得分:1)

在子查询中检查参数someParameter是否为空。

column3 = (from t3 in TableThree
           where 
              someParameter != null &&
              t3.id == "someId"
           select new SomeObject
           {
                Field1 = t3.Col1,
                Field2 = t3.Col2
           }).ToList()