Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq "join" with a IList<T> getting "Error Unable to create a constant value.."
    primarykey
    data
    text
    <p>I have a Save Method that saves with a Linq query a <em>manually re-orderd list</em> (in a web form) that is <em>passed as the parameter to my method</em>, and I try to update the <strong>Order Property</strong> of the <code>IEnumerable&lt;VM_CategoryLabel&gt;</code> I retrieve from the database (EF) with the corresponding value in the list (maybe would that be clearer with my code below):</p> <pre><code>public static void SaveFromList(IList&lt;VM_CategoryLabelExtra&gt; listTemplate) { int idCat = listTemplate.Select(x =&gt; x.IdCat).FirstOrDefault(); var test = (int)listTemplate.Where(z =&gt; z.Id == 8).Select(z =&gt; 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 =&gt; z.Id == x.ID_LabelPerso).Select(z =&gt; z.Order).First(), Label = x.arc_Label.Label, Unit = x.arc_Label.Unit }; context.SaveChanges(); } } </code></pre> <p>I used the <em>"test" var</em> 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:</p> <p><strong>Unable to create a constant value of type 'Namespace.Models.VM_CategoryLabelExtra. "Only primitive types and enumeration types are supported in this context.</strong></p> <p>Here are my classes:</p> <pre><code> 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; } } </code></pre> <p>So I suppose that I should not query the list inside my query ? So how do I "match" the 2 lists of values ?</p> <p>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:</p> <pre><code>foreach (var item in requete) { item.Order = listTemplate.Where(x =&gt; x.Id == item.Id).Select(x =&gt; x.Order).FirstOrDefault(); } try { context.SaveChanges(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload