C# LINQ在GROUPBY子句中使用局部变量
这可能真的很直截了当,但我似乎无法解决它。基本上,我有一个相当复杂的陈述,其中一部分如下所示:C# LINQ在GROUPBY子句中使用局部变量,c#,linq,C#,Linq,这可能真的很直截了当,但我似乎无法解决它。基本上,我有一个相当复杂的陈述,其中一部分如下所示: from a in context.ALLProducts join m in context.Mappings on a.LegislationID equals m.LegislationCode into map from subMap in map.DefaultIfEmpty() let mapping = (subMap.LegislationCode != null) ? subMap.
from a in context.ALLProducts
join m in context.Mappings on a.LegislationID equals m.LegislationCode into map
from subMap in map.DefaultIfEmpty()
let mapping = (subMap.LegislationCode != null) ? subMap.OldLegislationCode : a.LegislationID
...
group a by new { a.ProductCode, a.LanguageID, mapping, a.FormulaID } into g
该查询在映射表上进行链接,该表可能与LegirationId匹配,也可能与LegirationId不匹配,因此我进行了左连接,然后将结果存储在名为mapping的局部变量中,该变量将是映射值或来自第一个表(a)的值。现在我需要做的是在我的group语句中包含映射变量,但它没有被识别,大概是因为我在表a中分组
有人知道如何获得映射值,以便在对数据分组后使用它吗
非常感谢
Andrew在将值分配给
映射
变量之前,您应该验证子映射
是否有值:
from a in context.ALLProducts
join m in context.Mappings
on a.LegislationID equals m.LegislationCode into map
from subMap in map.DefaultIfEmpty()
let mapping = (subMap != null) ? // here
subMap.OldLegislationCode : a.LegislationID
group a by new { a.ProductCode, a.LanguageID, mapping, a.FormulaID } into g
select g;
嗨,这实际上不是问题所在-在你上面的例子中,我需要做的是在我完成组后访问映射值,即我取g,转换为列表,对于g中的每个项目,我希望能够访问名为“映射”的成员,它将包含旧的立法或当前的立法。@Andrew现在无法在数据库上验证,但使用Linq to Objects查询工作没有任何问题。您可以从分组的
键
访问映射
属性。如下所示:选择g.Key.mapping