我有一个linq查询在我的aspnet核心应用程序返回更多的行,那么它应该。查询如下。如何做一个linq组
public List<csAutoComplete> GetAutoComplete(string Type, string filter)
{
string[] GasCodesnotListed = { "F97", "F98", "F99" };
Model1 = from x in _context.TblGascodes
where ((x.GasCode.StartsWith("F13") || x.GasName.StartsWith("F13")) &&
!GasCodesnotListed.Contains(x.FedclusterCode))
orderby x.GasCode
group x by new csAutoComplete { ACCode = x.GasCode, ACName = x.GasName } into Alpha
select new csAutoComplete <=== (Is this the issue???)
{
ACCode = Alpha.Key.ACCode,
ACName = Alpha.Key.ACCode + " - " + Alpha.Key.ACName
};
return Model1.ToList();
}
} 回报(7)导致
所以我粘贴到LINQ垫,并得到了结果,我预计(1)
string[] GasCodesnotListed = { "F97", "F98", "F99" };
TblGasCodes
.Where (
x =>
((x.GasCode.StartsWith ("F13") || x.GasName.StartsWith ("F13")) &&
!(GasCodesnotListed.Contains (x.GasCode))
)
)
.OrderBy (x => x.GasCode)
.GroupBy (
x =>
new
{
ACCode = x.GasCode,
ACName = x.GasName
}
)
.Select (
Alpha =>
new
{
ACCode = Alpha.Key.ACCode,
ACName = ((Alpha.Key.ACCode + " - ") + Alpha.Key.ACName)
}
)
唯一的区别似乎是在新的csAutoComplete。如果这是一个类定义,为什么这应该有所作为?我该如何解决这个问题。
如果'csAutoComplete'没有覆盖'Equals',它将通过Object.ReferenceEquals()进行比较,而为了GroupBy的目的,匿名类型(如第二个示例中)是相同的,如果所有属性是相同的。我会使用匿名类型进行分组。 –
当你说匿名类型时,我不明白这一点。你的意思是创建新变量并删除类? –
在你的第二个版本中,你有'new ACCode = x.GasCode, ACName = x.GasName }' - 你正在创建一个匿名类型的实例。 –