Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to do join, group by and where condition using lambda expression in entity frame core but unable to return the query as IQuerayable. Below is the code -

_entities.Company.Join(_entities.Employee,
                             com => com.Company_id,
                             emp => emp.Company_id,
                            (com, emp) => new SearchCompany
                                         {
                                           companyID = com.Company_id,
                                           industryType = com.industry_id,
                                           company = com.Company_name,
                                           location = com.City,
                                           contacts = emp.Employee_phone
                                          })
                             .Where(input => input.company == companyName)
             .GroupBy(c => new { c.companyID, c.industryType, c.company, c.location })
                             .ToListAsync();


SearchCompany is a class with companyID,industryType,company,location,contacts as string properties and I need to return these values.

Please help me in getting it solved. Thanks in advance.

What I have tried:

I have tried in google but I am unable to find correct solution to return the query to IQueryable with join,group by and where conditions combination with lambda expression.
Posted
Updated 11-Jul-18 2:46am

1 solution

If you want it IQueryable then don't do the "ToListAsync" at the end.
 
Share this answer
 
Comments
sri4dotnet 11-Jul-18 8:56am    
Then its giving error as cannot convert IQueryabale<igrouping> to IQuerayable<searchcompany>. SearchCompany is my custom class
F-ES Sitecore 11-Jul-18 9:24am    
Think about the data being returned by your group. If your data is

1, a
2, b
2, c
3, d
3, e
3, f

then doing a GroupBy on the first item gives you a nested structure like

1
-- a
2
-- b
-- c
3
-- d
-- e
-- f

Do your group is three "Key" items (1, 2, 3) and each of those Keys is an IEnumerable of your actual data (SearchCompany), so you effectively have a list of lists.

So how do you want that nested structure to converted to a single list of SearchCompany objects? Rather than ToList you could do

.SelectMany(x => x.ToList())

That will give you just the SearchCompany objects but there are now no longer nested, so you've effectively discarded the groupby and you could have duplicate data. If you want a single list of SearchComapny items then you need to decide if you want the groupby and don't want a Distinct instead. Or you can do as above and return just the grouped data but that probably won't give you want you want either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900