1

我有一个保存方法,它使用 Linq 查询保存手动重新排序的列表(以 Web 表单形式),该列表作为参数传递给我的方法,我尝试更新从数据库中检索的Order 属性IEnumerable<VM_CategoryLabel>(EF ) 与列表中的相应值(也许我下面的代码会更清楚):

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

我使用“测试”变量来查看我的“子查询”是否获得了正确的值,并且确实如此,但是当我在 Select (注释的 Order 行)中使用我的 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 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; }
}

所以我想我不应该在我的查询中查询列表?那么我如何“匹配”这两个值列表呢?

我还尝试了以下方法(在 Linq 查询中替换后:Order = x.Sequence_Cat),因为迭代变量是只读的,所以这两种方法都不起作用:

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

                try
                {
                    context.SaveChanges();
4

2 回答 2

2

我建议使用这个

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

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

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

现在应该工作:)

于 2013-08-12T10:03:30.617 回答
0

让的例子:

// 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
}

如果您不知道 Let 的工作原理,请下载 LinqPad 并查看示例

于 2013-08-12T10:04:24.823 回答