Linq-To-Entities使用方法选择新对象

时间:2015-11-09 10:54:16

标签: c# asp.net-mvc entity-framework linq linq-to-entities

我尝试在我的应用程序中多次使用相同的选择查询。 例如,我有这个select语句:

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

但我也有:

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

正如您所看到的,我在两个linq select语句中使用完全相同的代码(对于SingleItemDTO)。有没有办法分离我的第一个select语句的代码(没有得到NotSupportedException)并重新使用它?我的第二个查询应如下所示:

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

2 个答案:

答案 0 :(得分:5)

这是可能的,但有点不寻常。

创建辅助方法并移动第一个查询的选择器部分,如下所示

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

现在第一个查询可以像这样

_db.tbl_itembundlecontents.Select(ToSingleItemDTO());

和第二个像这样

_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())
    });

注意导航属性后的AsQueryable(),这是使其工作的必要部分。

答案 1 :(得分:2)

这样的事情?

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