Linq “join” with a IList getting “Error Unable

2019-07-26 10:36发布

问题:

I have a Save Method that saves with a Linq query a manually re-orderd list (in a web form) that is passed as the parameter to my method, and I try to update the Order Property of the IEnumerable<VM_CategoryLabel> I retrieve from the database (EF) with the corresponding value in the list (maybe would that be clearer with my code below):

public static void SaveFromList(IList<VM_CategoryLabelExtra> listTemplate)
    {
        int idCat = listTemplate.Select(x => x.IdCat).FirstOrDefault();
        var test = (int)listTemplate.Where(z => z.Id == 8).Select(z => z.Order).FirstOrDefault();

        using (var context = new my_Entities())
        {
            var requete = from x in context.arc_CatLabel
                          where x.ID_Categorie == idCat
                          orderby x.Sequence_Cat
                          select new VM_CategoryLabel
                          {
                              Id = x.ID_LabelPerso,
                              //Order = x.Sequence_Cat,
                              Order = (int)listTemplate.Where(z => z.Id == x.ID_LabelPerso).Select(z => z.Order).First(),
                              Label = x.arc_Label.Label,
                              Unit = x.arc_Label.Unit
                          };
            context.SaveChanges();
        }
    }

I used the "test" var to see if my "sub-query" gets the correct value, and it does, but when I use my Linq expression inside the Select (the commented Order line), I get the following error:

Unable to create a constant value of type 'Namespace.Models.VM_CategoryLabelExtra. "Only primitive types and enumeration types are supported in this context.

Here are my classes:

       public class VM_CategoryLabel
{
    public int Id { get; set; }
    public int Order { get; set; }
    public string Label { get; set; }
    public string Unit { get; set; }
    public bool Checked { get; set; }
}

public class VM_CategoryLabelExtra
{
    public int Id { get; set; }
    public int IdCat { get; set; }      
    public int Order { get; set; }
    public string Label { get; set; }
    public string Unit { get; set; }
    public bool Checked { get; set; }
}

So I suppose that I should not query the list inside my query ? So how do I "match" the 2 lists of values ?

I also tried the following (after having replace in the Linq query: Order = x.Sequence_Cat)that is not working neither because the iteration variable is read-only:

foreach (var item in requete)
                {
                    item.Order = listTemplate.Where(x => x.Id == item.Id).Select(x => x.Order).FirstOrDefault();
                }

                try
                {
                    context.SaveChanges();

回答1:

I suggest using this.

It is the let clause.

public static void SaveFromList(IList<VM_CategoryLabelExtra> listTemplate)
    {
        int idCat = listTemplate.Select(x => x.IdCat).FirstOrDefault();
        var test = (int)listTemplate.Where(z => z.Id == 8).Select(z => z.Order).FirstOrDefault();

    using (var context = new my_Entities())
    {
        var requete = from x in context.arc_CatLabel
                      where x.ID_Categorie == idCat
                      orderby x.Sequence_Cat
                      let list = listTemplate                                     
                      select new VM_CategoryLabel
                      {
                          Id = x.ID_LabelPerso,                              
                          Order = list.Where(z => z.Id == x.ID_LabelPerso).Select(z => z.Order).First(),                             
                          Label = x.arc_Label.Label,
                          Unit = x.arc_Label.Unit
                      };
        context.SaveChanges();
    }
}

edit: instead offrom you can just do let list = listTemplate

Should work now :)



回答2:

example for let:

// The let keyword in query expressions comes in useful with subqueries: it lets
// you re-use the subquery in the projection:

from c in Customers
let highValuePurchases = c.Purchases.Where (p => p.Price > 1000)
where highValuePurchases.Any()
select new
{
    c.Name,
    highValuePurchases
}

If you do not know how Let working than please download LinqPad and see an example