System.Linq.Dynamic.Core : How do I make a select many correctly?

.net c# dynamic-linq linq

Question

I'm a rookie in the System. Linq.Dynamic. Core. I own this

Suppose we have:

Packs = new List<Pack>
{
    new Pack()
    {
        IdAtSource="Pack1",
        Equipments= new List<Equipment>()
        {
            new Equipment
            {
                Id=1,
                GenericEquipment = new GenericEquipment()
                {
                    Id=7
                }
            }
        }
    },
    new Pack()
    {
        IdAtSource="Pack2",
        Equipments= new List<Equipment>()
        {
            new Equipment
            {
                Id=2,
                GenericEquipment = new GenericEquipment()
                {
                    Id=1
                }
            },
            new Equipment
            {
                Id=2,
                GenericEquipment = new GenericEquipment()
                {
                    Id=2
                }
            }
        }
    }
}

I'd want to choose thePacks with Equipments nonetheless, in the chosenEquipments I must have the one with the number only.Id=2 for Universal Hardware. (The output should include a list of equipment-related packs.)

I've tried this

querable.Where("Packs.Equipments.Select((GenericEquipment.Id)=1)");

But I think I'm very off-target here. Is there a website with instructions on how to use this library?

Many thanks

1
2
11/13/2016 5:54:45 AM

Accepted Answer

The project here with the documentation here looks to be the most current version of Dynamic LINQ.


Typically, you should only use Dynamic LINQ when the types are known at compile time rather to conventional LINQ.

When compiling, if you anticipate that the query will be against aList<Pack> standard LINQ, as seen in the following code, which updates the initial instances ofPack ):

var usefulPacks = Packs.Select(pack => {
    pack.Equipments = pack.Equipments.Where(equipment => 
        equipment.GenericEquipment.Id == 1
    ).ToList(); 
}).Where(pack => pack.Equipments.Any()).ToList();

This code makes duplicates of the original instances if you require code that doesn't change the original ones:

var usefulPacks = Packs.Select(pack => {
    return new Pack() {
        IDAtSource = pack.IDAtSource,
        Equipments = pack.Equipments.Where(equipment => 
            equipment.GenericEquipment.Id == 1
        ).ToList(); 
    };
}).Where(pack => pack.Equipments.Any()).ToList();

Keep in mind that this isn't utilizing.SelectMany.SelectMany is used to combine many enumerables into one; in this case, eachPack the last item on the list refers to aPack originally on the list.


Modifying or initializing properties as part of an expression is not supported by dynamic LINQ:

The expression language permits getting (but not setting) the value of any reachable public field, property, or indexer.

so theEquipments attribute cannot be altered or set to only include instances ofEquipment that meet the requirements.

To determine theEquipments you have two options:

  1. Include a constructor inPack which considers the relevant arguments
  2. Create a static method for every class that accepts the necessary parameters.

Include a constructor inPack

You might include a constructor inPack With the proper justifications, that establishesEquipments :

Pack(int IDAtSource, IEnumerable<Equipment> equipments) {
    this.IDAtSource = IDAtSource;
    this.Equipments = equipments.ToList();
}

then you might use the subsequent:

IQueryable qry = Packs.AsQueryable();
qry = qry
    .Select("Pack(IDAtSource, Equipments.Where(GenericEquipment.ID=1))")
    .Where("Equipments.Any");

Establish a static method

public static class MyPackMethods {
    public static Pack Create(int IDAtSource, IEnumerable<Equipment> equipments) {
        return new Pack() {
            IDAtSource = IDAtSource,
            Equipments = equipments.ToList()
        };
    }
}

then dial:

IQueryable qry = Packs.AsQueryable();
qry = qry
    .Select("MyPackMethods.Create(IDAtSource, Equipments.Where(GenericEquipment.ID=1))")
    .Where("Equipments.Any");
0
12/3/2017 2:17:09 AM

Popular Answer

These should function:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex

Msdn Link: https://msdn.microsoft.com/en-us/library/bb397927.aspx (Sorry, I'm on my phone and can't rename it).



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow