Linq“加入”具有IList< T>获得“错误无法创建常量值”。 [英] Linq "join" with a IList<T> getting "Error Unable to create a constant value.."

查看:67
本文介绍了Linq“加入”具有IList< T>获得“错误无法创建常量值”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存方法,使用Linq查询将手动重新排序列表(在Web表单中)作为参数传递给我的方法,我尝试更新从数据库(EF)中获取的 IEnumerable< VM_CategoryLabel> 订单属性与列表中的相应值(可能会更清楚我的代码在下面):

  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();

使用(var context = new my_Entities())
{
var requete = from x in context.arc_CatLabel
其中x.ID_Categorie == idCat
orderby x.Sequence_Cat
选择新的VM_CategoryLabel
{
Id = x.ID_LabelPerso,
// Order = x.Sequence_Cat,
Order =(int)listTemplate。其中(z => z.Id == x.ID_LabelPerso).Select(z => z.Order).First(),
Label = x.arc_Label.Label,
Unit = x .arc_Label.Unit
};
context.SaveChanges();
}
}

我使用了testvar 来查看我的子查询是否获得正确的值,但是,当我在Select(已注释的订单行)中使用我的Linq表达式时,我会收到以下错误:



无法创建类型为Namespace.Models.VM_CategoryLabelExtra的常量值。



这是我的课程:

  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检查{get; set;}
}

public class VM_CategoryLabelExtra
{
public int Id {get; set;}
public int IdCat {get; set;}
public int Order {get; set;}
public string标签{get; set;}
public string Unit {get; set;}
public bool检查{get; set;}
}
/ pre>

所以我想我不应该查询我的查询中的列表,那么如何匹配2个值列表?



我还尝试了以下(在Linq查询中替换为Order = x.Sequence_Cat),这不是beca使用迭代变量是
只读:

  foreach(请求中的var项)
{
item.Order = listTemplate.Where(x => x.Id == item.Id).Select(x => x.Order).FirstOrDefault();
}

try
{
context.SaveChanges();


解决方案

我建议使用这个



这是 let 子句。

  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();

使用(var context = new my_Entities())
{
var requete = from x in context.arc_CatLabel
其中x.ID_Categorie == idCat
orderby x.Sequence_Cat
let list = listTemplate
选择新的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();
}
}

编辑:而不是 你可以做 let list = listTemplate



现在应该工作:)


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();

解决方案

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 :)

这篇关于Linq“加入”具有IList&lt; T&gt;获得“错误无法创建常量值”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆