4

I am using Dynamic Linq Library (System.Linq.Dynamic.dll) for dynamic data retrieval, But I am facing some problem in converting the below code to dynamic linq.

Consider I have following class A

public class A
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Data { get; set; }
}

And the following linq code also :

static void Main(string[] args)
{
    List<A> lst = new List<A>()
    {
        new A() { Id = 1, Name = "A", Data = 10 }, 
        new A() { Id = 1, Name = "B", Data = 15 },
        new A() { Id = 2, Name = "C", Data = 10 },
        new A() { Id = 2, Name = "D", Data = 15 }
    };      

    var d = lst.GroupBy(gp => gp.Id)
        .Select(
            sl =>
            new
            {
                Data = sl.Select(
                    s =>
                    new
                    {
                        data = s.Data,
                        id = s.Id,
                        Name = s.Name
                    })
                         .Union(sl.Select(
                             sd =>
                             new
                             {
                                 data = sl.Sum(s => s.Data),
                                 id = sl.Key,
                                 Name = ""
                             }))
            })
        .SelectMany(sl => sl.Data).ToList();
}

How can I achieve the same using Dynamic Linq library?

Below is the dynamic code I have tried.

var d = res.GroupBy(qry.GroupByColumn, "it").Select("it").Cast<IQueryable<object>>()
    .Select(
        sl =>
        new
        {
            data = sl.Select(selectString)
                     .Union(sl.Select(
                         sd =>
                         new
                         {
                             data = sl.Select(
                                "new(Min(" + qry.GroupByColumn + ") as " 
                                + qry.GroupByColumn + "," + sumQry + ",1 as IsTotal)")
                         }))
    }).ToList();

But when I run the code I am getting the following exception

Unable to cast the type 'System.Linq.IGrouping`2
[[System.String, mscorlib, Version=4.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], [DynamicClass1, DynamicClasses, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]'
to type 'System.Linq.IQueryable`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

LINQ to Entities only supports casting EDM primitive or enumeration types.

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
Harisyam M
  • 407
  • 1
  • 3
  • 14
  • 2
    What _exactly_ is this "some problem" you are facing? – René Vogt Dec 18 '17 at 13:13
  • this is an example I created using List. But I want to achieve the same for IQueryable – Harisyam M Dec 18 '17 at 14:12
  • Hi @René Vogt thanks for the comment. I have added the code and exception details to the question. – Harisyam M Dec 18 '17 at 14:20
  • Can you post the dynamic linq query you have tried and where you get stuck? – Kasper van den Berg Dec 18 '17 at 14:24
  • Hi @KaspervandenBerg updated the question with the code I have tried. – Harisyam M Dec 18 '17 at 14:29
  • Can you show what 'res' and 'qry' are? It appears you are trying to combine IGrouping, which is from linq-to-objects as far as i know, with IQueryable. Which does not work. You might be able to use AsQueryable() somewhere in the expression, but this does not automattically send the query to the database. However, I do not know that much about dynamic linq and i do not have my compiler or c# repl with me; i hope that when you provide more details, someone can answer your question. – Kasper van den Berg Dec 18 '17 at 14:49
  • Hi @KaspervandenBerg Res is an IQueryable data which is dynamic let's say it is dbContext.Employees.where(wh=>wh.Emp_id==1001); And qry is an object which I maintain to keep all my select columns, Filter columns, Group by columns and Sort columns because in Dynamic Linq we can the query very dynamically using column names. All these columns are retrieved from db – Harisyam M Dec 18 '17 at 15:14
  • What do you think this code `.Select("it").Cast>()` is accomplishing for you? – NetMage Dec 18 '17 at 22:46

0 Answers0