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

问题描述 投票:0回答:3

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

 _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()
                });
c# asp.net-mvc entity-framework linq linq-to-entities
3个回答
8
投票

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

创建一个辅助方法并像这样移动第一个查询的 selector 部分

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()
,这是使其正常工作的基本部分。


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

0
投票

或者类似的东西?

public object[] BicagiKullanabilenMakinalar { get; set; }

item.BicagiKullanabilenMakinalar = await (from bicak in _dbContext.BicakMakinas
                                      join islem in _dbContext.Islems on bicak.Islemkod equals islem.Islemkod into islemInto
                                      from islem in islemInto.DefaultIfEmpty()
                                      join makina in _dbContext.MakinaKarts on bicak.Makinakodu equals makina.Makinakodu into makinaInto
                                      from makina in makinaInto.DefaultIfEmpty()
                                      where islem.Islemtipkod == "002"
                                      && bicak.Bicakkodu == bicakKodu
                                      select new
                                      {
                                          MakinaKodu = bicak.Makinakodu,
                                          MakinaAdi = makina.Makinaadi,
                                          KodAciklama = islem.Kodaciklama
                                      })
                                      .Distinct()
                                      .OrderBy(e => e.MakinaAdi)
                                      .ToArrayAsync(cancellationToken);

回应;

   "bicagiKullanabilenMakinalar": [
        {
            "makinaKodu": "003",
            "makinaAdi": "GALLUS R200B",
            "kodAciklama": "BIÇAKLAMA (TAHTA DISLI)"
        },
        {
            "makinaKodu": "004",
            "makinaAdi": "ZONTEN",
            "kodAciklama": "BIÇAKLAMA (TAHTA DISLI)"
        }
    ],
© www.soinside.com 2019 - 2024. All rights reserved.