Linq-To-Entities using method to select new object

早过忘川 提交于 2020-12-09 08:05:50

问题


I try to use the same select query multiple times in my application. For example I have this select statement:

 _db.tbl_itembundlecontents
                    .Select(z => new SingleItemDTO
                    {
                        amount = z.amount,
                        id = z.id,
                        position = z.position,
                        contentType = new BasicMaterialDTO
                        {
                            id = z.tbl_items.id,
                            img = z.tbl_items.tbl_material.img,
                            name = z.tbl_items.tbl_material.name,
                            namekey = z.tbl_items.tbl_material.namekey,
                            info = z.tbl_items.tbl_material.info,
                            weight = z.tbl_items.tbl_material.weight
                        }
                    });

but i also have this:

 _db.tbl_itembundle
                .Where(x => x.type == 2)
                .Select(y => new ItemBundleDTO
                {
                    name = y.name,
                    namekey = y.namekey.
                    contents = y.tbl_itembundlecontents
                    .Select(z => new SingleItemDTO
                    {
                        amount = z.amount,
                        id = z.id,
                        position = z.position,
                        contentType = new BasicMaterialDTO
                        {
                            id = z.tbl_items.id,
                            img = z.tbl_items.tbl_material.img,
                            name = z.tbl_items.tbl_material.name,
                            namekey = z.tbl_items.tbl_material.namekey,
                            info = z.tbl_items.tbl_material.info,
                            weight = z.tbl_items.tbl_material.weight
                        }
                    })
                });

As you can see I use exactly the same code (for the SingleItemDTO) in both linq select statements. Is there a way to separate the code of my first select statement (without getting the NotSupportedException) and reuse it again? My second query should look like this:

_db.tbl_itembundle
                .Where(x => x.type == 2)
                .Select(y => new ItemBundleDTO
                {
                    name = y.name,
                    namekey = y.namekey.
                    contents = y.tbl_itembundlecontents.ToDTO()
                });

回答1:


It's possible, but in a bit unusual way.

Create a helper method and move the selector part of your first query like this

static Expression<Func<[tbl_itembundlecontents entity type], SingleItemDTO>> ToSingleItemDTO()
{
    return z => new SingleItemDTO
    {
        amount = z.amount,
        id = z.id,
        position = z.position,
        contentType = new BasicMaterialDTO
        {
            id = z.tbl_items.id,
            img = z.tbl_items.tbl_material.img,
            name = z.tbl_items.tbl_material.name,
            namekey = z.tbl_items.tbl_material.namekey,
            info = z.tbl_items.tbl_material.info,
            weight = z.tbl_items.tbl_material.weight
        }
    };
}

Now the first query can be like this

_db.tbl_itembundlecontents.Select(ToSingleItemDTO());

and the second like this

_db.tbl_itembundle
    .Where(x => x.type == 2)
    .Select(y => new ItemBundleDTO
    {
        name = y.name,
        namekey = y.namekey.
        contents = y.tbl_itembundlecontents.AsQueryable().Select(ToSingleItemDTO())
    });

Note the AsQueryable() after the navigation property, this is the esential part to make it work.




回答2:


something like this?

public static class HelperExtension
{
    public static IEnumerable<SingleItemDTO> ToDto(this IEnumerable<Tbl_itembundlecontents> items)
    {
        return items.Select(z => z.ToDto());
    }

    public static SingleItemDTO ToDto(this Tbl_itembundlecontents z)
    {
        return new SingleItemDTO
        {
            amount = z.amount,
            id = z.id,
            position = z.position,
            contentType = new BasicMaterialDTO
            {
                id = z.tbl_items.id,
                img = z.tbl_items.tbl_material.img,
                name = z.tbl_items.tbl_material.name,
                namekey = z.tbl_items.tbl_material.namekey,
                info = z.tbl_items.tbl_material.info,
                weight = z.tbl_items.tbl_material.weight
            }
        };
    }
}


来源:https://stackoverflow.com/questions/33607575/linq-to-entities-using-method-to-select-new-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!