带有导航列表的EF Lambda Linq查询

时间:2012-12-20 13:53:12

标签: c# linq entity-framework linq-to-entities

我似乎无法用正确的选择完成我的linq语句。 到目前为止,我有以下内容,但问题是当我想要对所有内容进行选择时,它会导致只选择类别类型的“选择”:

var qwe = dc.LeadTypes
            .Include("DataTypes")
            .Include("DataTypes.CategoryTypes")
            .Where<LeadType>(x => x.Disabled == false)
            .SelectMany(x => x.DataTypes)
            .Where(x => x.Disabled == false)
            .SelectMany(x => x.CategoryTypes)
            .Where(x => x.Disabled == false)
            .ToList();

结果SQL

SELECT 
[Extent3].[CategoryTypeID] AS [CategoryTypeID], 
[Extent3].[Name] AS [Name], 
[Extent3].[DataTypeID] AS [DataTypeID], 
[Extent3].[Disabled] AS [Disabled]
FROM   [LeadLookup].[LeadType] AS [Extent1]
INNER JOIN [LeadLookup].[DataType] AS [Extent2] ON [Extent1].[LeadTypeID] = [Extent2].    [LeadTypeID]
INNER JOIN [LeadLookup].[CategoryType] AS [Extent3] ON [Extent2].[DataTypeID] =      [Extent3].[DataTypeID]
WHERE (0 = [Extent1].[Disabled]) AND (0 = [Extent2].[Disabled]) AND (0 = [Extent3].  [Disabled])

我这样做的原因是因为我在三个表中都有一个禁用列,我不想返回这些行。

由于

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解您的要求,但尝试通过启用所有实体来返回LeadTypes

var qwe = (from lt in dc.LeadTypes
           where !lt.Disabled
           select new LeadType
           {
               DataTypes = from dt in lt.DataTypes
                           where !dt.Disabled
                           select new DataType
                           {
                               CategoryTypes = dt.CategoryTypes.Where(ct => !ct.Disabled),
                               // rest of dt values
                           }
               // rest of lt values
           }).ToList();

答案 1 :(得分:0)

如果使用include(),则无法将列投影到不同的形状。 投影列不能具有相同的名称。

result

var qwe = dc.LeadTypes.Where(x => x.Disabled == false)
        .SelectMany(x => x.DataTypes.Where(y => y.Disabled == false), (lead, data) => new
        {
            disabledLeadType = lead.Disabled,
            disabledDataType = data.Disabled,
            idDataType = data.Id,
            categoryType = data.CategoryTypes
        }
        )
        .SelectMany(x => x.categoryType.Where(y => y.Disabled == false), (lead_data, category) => new
        {
        lead_data.disabledLeadType, lead_data.disabledDataType, lead_data.idDataType,
        disabledCategoryType = category.Disabled,
        idCatergoryType = category.Id
        }
        )
        .ToList();