3

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

5 回答 5

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

这是我如何使用它的示例 - dogs 是 Dog 的集合:

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

var dogsSelected = dogQuery.GetByFieldFilterObjectValue(dogs, "Age", 2);
于 2014-04-26T10:41:00.570 回答
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),
                })                  
        }); 
于 2014-04-25T07:06:09.083 回答
0

您不能在中使用它x.GetType()...linq query因为它不会被翻译成expression tree,只能使用linq您的提供者可以将它们翻译成侧面expression tree运行的方法,您可以从我添加的帖子(此处Data Base中阅读有关这些的更多信息。

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

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

或者

... var qbase = db.class1.ToList(); ....
于 2014-04-25T10:08:53.380 回答
0

使用 Co. Aden 的答案并对其进行修改。在选择之前使用 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),
            })                  
    }); 
于 2015-10-07T21:32:00.763 回答
-1

试试这个。

var qbase2 = db.class1.select( x => new { x.Id, x.Code,x.Name}).ToList();
于 2014-04-25T07:43:55.007 回答