0

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

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])

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

谢谢

4

2 回答 2

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();
于 2012-12-20T14:16:59.557 回答
0

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

结果

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();
于 2016-04-21T07:04:09.637 回答