Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-To-Entities using method to select new object

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()
                });
like image 623
yDeveloper Avatar asked Jan 08 '23 05:01

yDeveloper


1 Answers

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.

like image 120
Ivan Stoev Avatar answered Jan 09 '23 19:01

Ivan Stoev