简体   繁体   English

Select 使用 LINQ 与 db 不同的列名

[英]Select distinct column name from db using LINQ

I have a table that has several columns including CarMake, CarModel, CarModelLoading etc. I want to select distinct CarMake and return them as list.我有一个表,其中有几列,包括 CarMake、CarModel、CarModelLoading 等。我想 select 不同的 CarMake 并将它们作为列表返回。 I have tried the following:我尝试了以下方法:

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;
}

but whenever run the app, I get the error:但是每当运行该应用程序时,我都会收到错误消息:

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]'. 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;
     }

It gives me the error below:它给了我以下错误:

FirstOrDefault()' could not be translated. FirstOrDefault()' 无法翻译。 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'.以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

all the solutions given here haven't answered my query so far到目前为止,这里给出的所有解决方案都没有回答我的问题

Either declare your method to return an IE of string:要么声明您的方法以返回字符串的 IE:

public IEnumerable<string> GetAllMakes()

And your query to not make an anonymous type but simply select the make:而您的查询不是创建匿名类型,而只是 select 的品牌:

return context.EbimaCarMakesAndModels.Select(l => l.CarMake).Distinct().ToList();

Or group by make and return some nominal entry from the group list (but that doesn't really make sense to me)或按 make 分组并从组列表中返回一些名义条目(但这对我来说真的没有意义)

return context.EbimaCarMakesAndModels.GroupBy(x => x.CarMake)
       .Select(x => x.FirstOrDefault()).ToList();

Your last query introduced some additional unmentioned constraint .Where(v => v.Listed == "Y") - perhaps consider doing this on EbimaCarMakesAndModels before you group您的最后一个查询引入了一些额外的未提及的约束.Where(v => v.Listed == "Y") - 也许在分组之前考虑在EbimaCarMakesAndModels上执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM