LINQ to Entity-在查询中使用匿名对象 [英] LINQ to Entity - using anonymous object in a query

查看:78
本文介绍了LINQ to Entity-在查询中使用匿名对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写LINQ2Entity查询,该查询将仅返回表中的3列,然后将结果设置为 ComboBox DataSource .

I am trying to write LINQ2Entity query which will return only 3 columns from the table and then to set the result as a DataSource of a ComboBox.

我的问题是这个.首先-要从表中选择所有内容,我将使用以下服务:

The problem I have is this. First - to select all from the table I use a service like this:

IList<SoleColor> soles = SoleColorService.All().ToList();

已准备好将其设置为等待此类数据的ComboBox的数据源.但是,因为我想选择匿名对象来仅存储查询结果中的3列,所以我这样修改了查询:

Which is ready to be set as a DataSource for the ComboBox which waits data of this type. However because I want to choose anonymous object to store only 3 of the columns that the query result I modified my query like this :

IList<SoleColor> soles = SoleColorService.All()
                    .GroupBy(t => t.Sole.Code)
                    .Select(g => new
                    {
                        SoleCode = g.Key,
                        SoleName = g.Select(t => t.Sole.Name),
                        SoleId   = g.Select(t => t.SoleID)
                    }); 

这导致将 .Select 标记为错误,并带有以下文本无法将类型System.Linq.IQueryable隐式转换为System.Collections.Generic.IList".如果我更改 IList< SoleColor>soles = var soles = 很好,但是分配匿名类型(例如DataSource)存在问题,但是我认为可以在查询中解决此问题,因此可以在其中找到正确的类型.同样在某些时候,我必须向y查询添加 First() FirstOrDafult()之类的东西,因为我有很多记录具有相同的 SoleCode ,而我只想为每个唯一的 SoleCode 保留一个.

Which leads to marking the .Selectas error with the following text "Cannot implicitly convert type System.Linq.IQueryable to System.Collections.Generic.IList". If I change IList<SoleColor> soles = to var soles = it's fine but then there's a problem with assigning anonymous type like a DataSource, but I think this can be fixed in the query so I get the right type there. Also at some point I have to add something like First() or FirstOrDafult() to y query, because I have many records with the same SoleCode and I want to keep only one for each unique SoleCode.

推荐答案

第一个:如何仅在每个组中获得一个结果我已经在上一个问题中向您展示过.结合使用匿名类型,它将类似于以下内容:

First: How to only get one result per group I already showed you in your previous question. Combined with an anonymous type this would look like the following:

SoleColorService.All()
                .GroupBy(t => t.Sole.Code)
                .Select(g => g.First())
                .Select(x => new
                {
                    SoleCode = x.Sole.Code,
                    SoleName = x.Sole.Name),
                    SoleId   = x.SoleID)
                }); 

第二个:如果您需要以强类型化的方式从您的方法返回此查询的结果,则不能使用匿名类型.您将必须创建一个命名类型并使用它:

Second: If you need to return the result of this query in a strong typed manner from your method you can't use an anonymous type. You will have to create a named type and use that instead:

public class SoleModel
{
    private readonly string _code;
    private readonly string _name;
    private readonly int _id;

    public SoleModel(string code, string name, int id)
    {
        _code = code;
        _name = name;
        _id = id;
    }

    public string Code { get { return _code; } }
    public string Name { get { return _name; } }
    public int Id { get { return _id; } }
}

SoleColorService.All()
                .GroupBy(t => t.Sole.Code)
                .Select(g => g.First())
                .Select(x => new SoleModel(x.Sole.Code, x.Sole.Name x.SoleID)); 

第三:将匿名类型列表分配给数据源应该可以.

Third: Assigning a list of anonymous types to a datasource should work.

var result = SoleColorService.All()
                             .GroupBy(t => t.Sole.Code)
                             .Select(g => g.First())
                             .Select(x => new
                             {
                                 SoleCode = x.Sole.Code,
                                 SoleName = x.Sole.Name),
                                 SoleId   = x.SoleID)
                             }); 
someControl.DataSource = result;

这篇关于LINQ to Entity-在查询中使用匿名对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆