3

I want select Col1 or Col2 by dynamic in linq command for example

var temp1= "Col1";

thank you for answer

I try by

((ICollection<SanadItem>)x.GetType().GetProperty(temp1).GetType()).Select(g =>...

but get error LINQ to Entities does not recognize the method 'System.Type GetType()' method

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),
                })                  
        }); 
Harshana Narangoda
  • 775
  • 1
  • 8
  • 23
khoshghadam
  • 89
  • 1
  • 10

5 Answers5

0

Try it this way

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),
                })                  
        }); 
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression. – khoshghadam Apr 25 '14 at 07:20
0

you can not use this x.GetType()... in linq query, because it will not be translated to expression tree, only methods can be used in linq which your provider can translate them to expression tree to run on Data Base side, you can read more about these, from post i added (Here).

and about your question, if you insist on to use reflection, you have to fetch all of your data from data base first, and for this, you need to add .ToList() before your .Select() :

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

OR

... var qbase = db.class1.ToList(); ....
Community
  • 1
  • 1
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
0

I think the following works:

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

here is a sample of how I use it - dogs is a collection of Dog:

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

var dogsSelected = dogQuery.GetByFieldFilterObjectValue(dogs, "Age", 2);
ozidom
  • 159
  • 1
  • 5
  • thank you for answer but I want get IQueryable no IEnumerable because Linq do on Database and database have very record – khoshghadam Apr 26 '14 at 20:48
0

Using answer from Co. Aden and Modifying it. Use AsEnumerable() before select.

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),
            })                  
    }); 
vohrahul
  • 1,123
  • 10
  • 17
-1

Try This one.

var qbase2 = db.class1.select( x => new { x.Id, x.Code,x.Name}).ToList();
Harshana Narangoda
  • 775
  • 1
  • 8
  • 23