例如,我想在 linq 命令中通过动态选择 Col1 或 Col2
var temp1= "Col1";
谢谢你的答案
我试试
((ICollection<SanadItem>)x.GetType().GetProperty(temp1).GetType()).Select(g =>...
但得到错误 LINQ to Entities 无法识别方法 'System.Type GetType()' 方法
public class class1 {
public int Id {get; set;}
public string Name {get; set;}
public string Code {get; set;}
public ICollection<Class2> Col1 {get; set;}
public ICollection<Class2> Col2 {get; set;}
}
public class class2 {
public int Id {get; set;}
public int Id2 {get; set;}
public int Bed {get; set;}
public int Bes {get; set;}
}
...
..
.
public class mycontext:dbcontext{
public DbSet<Class1> class1 { get; set; }
public DbSet<Class2> class2 { get; set; }
}
var db = new mycontext()
var qbase = db.class1;
var qbase2 = qbase.Select(x => new
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
//--> I want select x.Col1 or x.Col2 by Dynamic but I cannot use library Linq.Dynamic
items = x.Col1.Where(xx => xx.Id2 == x.Id).GroupBy(b=>b.Id2).Select(y =>
new
{
Bed = y.Sum(c=>c.Bed),
Bes = y.Sum(c => c.Bes),
})
});