Select 使用 LINQ 来自数据库的不同列名
Select distinct column name from db using LINQ
我有一个 table,它有几个列,包括 CarMake、CarModel、CarModelLoading 等。我想 select 区分 CarMake 和 return 它们作为列表。
我尝试了以下方法:
public IEnumerable<EbimaCarMakesAndModel> GetAll()
{
//var list<ebima>
return (IEnumerable<EbimaCarMakesAndModel>)context.EbimaCarMakesAndModels.Select(l => new { l.CarMake }).Distinct().ToList();
//var data = (from dbo in context.EbimaCarMakesAndModels where dbo.Listed == "Y" select dbo.CarMake).Distinct().OrderBy(name => name).ToList();
//return (IEnumerable<EbimaCarMakesAndModel>)data;
}
但每当 运行 应用程序时,我都会收到错误消息:
An unhandled exception occurred while processing the request.
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType31
1[System.String]]' to type 'System.Collections.Generic.IEnumerable`1[motor_backend.Models.EbimaCarMakesAndModel]'.
I have also tried the following but to no avail:
public IEnumerable<EbimaCarMakesAndModel> GetAll()
{
var res = context.EbimaCarMakesAndModels.GroupBy(x => new { x.CarMake }).
Select(x => x.FirstOrDefault()).Where(v => v.Listed == "Y").ToList();
return res;
}
它给我以下错误:
FirstOrDefault()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
到目前为止,这里给出的所有解决方案都没有回答我的问题
将您的方法声明为 return 字符串的 IE:
public IEnumerable<string> GetAllMakes()
你的查询不是匿名类型,而是简单地 select make:
return context.EbimaCarMakesAndModels.Select(l => l.CarMake).Distinct().ToList();
或者按品牌和 return 组列表中的一些名义条目进行分组(但这对我来说真的没有意义)
return context.EbimaCarMakesAndModels.GroupBy(x => x.CarMake)
.Select(x => x.FirstOrDefault()).ToList();
你的最后一个查询引入了一些额外的未提及的约束 .Where(v => v.Listed == "Y")
- 也许考虑在你分组
之前在 EbimaCarMakesAndModels
上这样做
我有一个 table,它有几个列,包括 CarMake、CarModel、CarModelLoading 等。我想 select 区分 CarMake 和 return 它们作为列表。 我尝试了以下方法:
public IEnumerable<EbimaCarMakesAndModel> GetAll()
{
//var list<ebima>
return (IEnumerable<EbimaCarMakesAndModel>)context.EbimaCarMakesAndModels.Select(l => new { l.CarMake }).Distinct().ToList();
//var data = (from dbo in context.EbimaCarMakesAndModels where dbo.Listed == "Y" select dbo.CarMake).Distinct().OrderBy(name => name).ToList();
//return (IEnumerable<EbimaCarMakesAndModel>)data;
}
但每当 运行 应用程序时,我都会收到错误消息:
An unhandled exception occurred while processing the request. InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List
1[<>f__AnonymousType31
1[System.String]]' to type 'System.Collections.Generic.IEnumerable`1[motor_backend.Models.EbimaCarMakesAndModel]'. I have also tried the following but to no avail:
public IEnumerable<EbimaCarMakesAndModel> GetAll()
{
var res = context.EbimaCarMakesAndModels.GroupBy(x => new { x.CarMake }).
Select(x => x.FirstOrDefault()).Where(v => v.Listed == "Y").ToList();
return res;
}
它给我以下错误:
FirstOrDefault()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
到目前为止,这里给出的所有解决方案都没有回答我的问题
将您的方法声明为 return 字符串的 IE:
public IEnumerable<string> GetAllMakes()
你的查询不是匿名类型,而是简单地 select make:
return context.EbimaCarMakesAndModels.Select(l => l.CarMake).Distinct().ToList();
或者按品牌和 return 组列表中的一些名义条目进行分组(但这对我来说真的没有意义)
return context.EbimaCarMakesAndModels.GroupBy(x => x.CarMake)
.Select(x => x.FirstOrDefault()).ToList();
你的最后一个查询引入了一些额外的未提及的约束 .Where(v => v.Listed == "Y")
- 也许考虑在你分组
EbimaCarMakesAndModels
上这样做