0

I have a c# code as follows

var deptSalesQuery = 
    from d in db.DashboardFigures
    join s in outlets.Split(',').Select(x => int.Parse(x)) on d.OutletNo equals s
    where (d.TypeOfinformation == "SALES")
    group d by new
    {
        d.Number
    } into newGroupedresult
    select new DeptSales()
    { 
        Dn =   (int)newGroupedresult.Key.Number,
        Qs = (double)newGroupedresult.Sum(d => d.Value_4),
        Se = (double)newGroupedresult.Sum(d => d.Value_2),
        Si = (double)newGroupedresult.Sum(d => d.Value_3) 
           + (double)newGroupedresult.Sum(d => d.Value_2)
    };

When I pass in Outlets = "1,2,3,4,....all the way upto 110", the software crashes telling me that there are too many nested statements.

Is there any way that I can remove the JOIN and add something onto the WHERE clause to help the situation?

Thanks

juharr
  • 31,741
  • 4
  • 58
  • 93
Alpesh
  • 31
  • 1
  • 10
  • 3
    You can use the `Contains` method to check if `outlets.Split(',').Select(x => int.Parse(x))` contains the `d.OutletNo` – Mahesh Mar 12 '15 at 12:28
  • Something like `where outlets.Split(',').Select(int.Parsex).Contains(d.OutletNo) && d.TypeOfinformation == "SALES"` – xanatos Mar 12 '15 at 12:30
  • In EF there doesn't seem to be a maximum limit for `Contains` (http://stackoverflow.com/questions/8898564/entity-framework-hitting-2100-parameter-limit). Are you using EF or Linq-to-SQL? – xanatos Mar 12 '15 at 12:31
  • I'd start by doing the splitting and parsing outside of the main Linq query. – juharr Mar 12 '15 at 12:36
  • @xanatos , Im using EF. Also I tried the statement in the WHERE clause, but the error is that LINQ to Entities does not recognise Split – Alpesh Mar 12 '15 at 12:50

1 Answers1

1

As written by juharr, before the query do:

int[] splitted = outlets.Split(',').Select(int.Parse);

and in the query:

where splitted.Contains(d.OutletNo) && d.TypeOfinformation == "SALES"
xanatos
  • 109,618
  • 12
  • 197
  • 280