我有一个作为参数传递给我的方法传递一个保存方法与LINQ的保存查询手动重新orderd列表 (在网页形式),我尝试更新的顺序属性 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();
}
}
我用“试验”无功 ,看看我的“子查询”得到正确的值,这样做,但是当我使用的选择(该评论令行)在我的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; }
}
所以我想,我不应该查询我的查询里面的名单? 那么,如何“投其所好”值2个列表?
我也试过以下(具有LINQ查询替换后:订单= 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();