我遇到一些麻烦找到正确的语法来完成以下任务:
是否有可能与LINQ(Lambda表达式),以.GroupBy数据,而不是使用通常的.SUM()或.Count之间的()我想要得到的数据为int的List。
我定义我命名自己的类:Filter_IDs。 它的构造函数需要两个参数:
public int? type; // Represents the object_type column from my database
public List<int?> objects; // Represents the object_id column from my database
我想从我的数据库将数据加载到该对象。 下面的LINQ查询应导致Filter_IDs的列表:
下面的LINQ查询应导致Filter_IDs的列表:
List<Filter_IDs> filterids = ef.filterLine
.GroupBy(fl => fl.objectType)
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();
使用此查询没有给出建筑物错误,但给出了运行时“NotSupportedException异常”。
该数据库是这样的,给你一个更好地了解数据:
http://d.pr/i/mnhq+ (droplr图像)
在此先感谢,戈
我认为这个问题是DB不能够调用ToList的选择,也不是为了创建一个新的过滤器_id。
尝试是这样的:
List<Filter_IDs> filterids = ef.filterLine.Select(o => new { objectType = o.objectType, object_id=o.object_id})
.GroupBy(fl => fl.objectType).ToList()
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();
也许你想
IList<Filter_IDs> filterIds = ef.filterline
.Select(fl => fl.objectType).Distinct()
.Select(ot => new Filter_IDs
{
type = ot,
objects = ef.filterline
.Where(fl => fl.objectType == ot)
.Select(fl =>objectType)
.ToList()
}).ToList();
获取不同名单objectType
,并用它来子查询的每个列表object_id
。
然而,它似乎更高效的我只是列举,以便值,
var results = new List<Filter_IDs>();
var ids = new List<int>();
var first = true;
int thisType;
foreach (var fl in ef.filterLines
.OrderBy(fl => fl.objectType)
.ThenBy(fl => fl.object_Id))
{
if (first)
{
thisType = fl.objectType;
first = false;
}
else
{
if (fl.objectType == thisType)
{
ids.Add(fl.object_Id);
}
else
{
results.Add(new Filter_IDs
{
Type = thisType,
objects = ids
});
thisType = fl.objectType;
ids = new List<int>();
}
}
}
您可以在客户端使用的GroupBy:
List<Filter_IDs> filterids = ef.filterLine
.Select(fl=>new {fl.ObjectType, fl.object_id})
.AsEnumerable()
.GroupBy(fl => fl.objectType)
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();