linq选择集合列动态

时间:2014-04-25 06:57:47

标签: c# linq linq-to-sql linq-query-syntax

我想在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),
                })                  
        }); 

5 个答案:

答案 0 :(得分:0)

以这种方式试试

var qbase2 = qbase.Select(x => new
        {
            Id = x.Id,
            Code = x.Code,
            Name = x.Name,
            DynamicCol = (int)s.GetType().GetProperty("Col1").GetValue(x, null) ,
            //Remember to cast the dynamic column according to its datatype
            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),
                })                  
        }); 

答案 1 :(得分:0)

试试这个。

var qbase2 = db.class1.select( x => new { x.Id, x.Code,x.Name}).ToList();

答案 2 :(得分:0)

您无法在x.GetType()...中使用此linq query,因为它不会被翻译为expression tree,只有linq可以使用您的提供商可以翻译它们的方法要expression treeData Base方运行,您可以从我添加的帖子(Here)中了解更多相关内容。

关于您的问题,如果您坚持使用reflection,则必须先从data base获取所有数据,为此,您需要先添加.ToList()你的.Select()

... var qbase2 = qbase.ToList().Select(x => new ...

OR

... var qbase = db.class1.ToList(); ....

答案 3 :(得分:0)

我认为以下工作:

public class DynamicQuery<T>
    {
        public IEnumerable<T> GetByFieldFilterObjectValue(IEnumerable<T> collection, string colName, object value)
        {
            IEnumerable<T> results = collection.Where(d => d.GetType().GetProperty(colName).GetValue(d, null).Equals(value));

            return results;
        }
    }

这里是我如何使用它的样本 - 狗是狗的集合:

DynamicQuery<Dog> dogQuery = new DynamicQuery<Dog>();

var dogsSelected = dogQuery.GetByFieldFilterObjectValue(dogs, "Age", 2);

答案 4 :(得分:0)

使用Co. Aden的回答并修改它。在select之前使用AsEnumerable()。

var qbase2 = qbase.AsEnumerable().Select(x => new
    {
        Id = x.Id,
        Code = x.Code,
        Name = x.Name,
        DynamicCol = (int)s.GetType().GetProperty("Col1").GetValue(x, null) ,
        //Remember to cast the dynamic column according to its datatype
        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),
            })                  
    });