Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

LINQ count with group by from SharePoint list items.

$
0
0

Hi,

I need some aggregate data from SharePoint lists which has a common column as primary and foreign key (lookup column)I have two lists called Entity and Sector. Each entity reports to a Sector and few entities do not report to any one.

Entities have Risk column which can have one of Risk level. Either High, Medium or Low. I want a result set which will tell me entities count by Risk per Sector.

I am able to get correct total entities count from my first query but unable to group them and get the count by risk as SQL query doing in snapshot below.

I successfully got the result from SQL and this is what is expected from LINQ. Either query or tools to generate LINQ would be helpful.

SQL query with correct expected result

incomplete LINQ query

incomplete LINQ query result

LINQ query

var result = (from SPListItem e in lstEntity.Items
                                      join SPListItem s in lstSRA.Items on new SPFieldLookupValue(Convert.ToString(e["Reporting_x0020_SRA_x003a_ID"])).LookupId equals s.ID
                                      where
                                          Convert.ToBoolean(e["IsActive"]) == true&& Convert.ToDateTime(e["CenterApprovalDate"]) <= startDate
                                      select new
                                      {
                                          SRAId = s.ID,
                                          Sector = new SPFieldLookupValue(Convert.ToString(s["Sector:Title"])).LookupValue,
                                          LicensedEntities = s["LicensedEntities"] != null ? Convert.ToInt32(s["LicensedEntities"]) : 0,
                                          Risk = new SPFieldLookupValue(Convert.ToString(e["Risk:Title"])).LookupValue
                                      }
                                     ).ToList();
select sec.[Sector:Title] as 'Sector',count (enty.id) 'Total Entities', 
sum(case when enty.[Risk:Title] = 'High' then 1  else 0 end) as 'High Risk' ,
sum(case when enty.[Risk:Title] = 'Medium' then 1  else 0 end) as 'Medium Risk',
sum(case when enty.[Risk:Title] = 'Low' then 1  else 0 end) as 'Low Risk'
from Entity enty left join SRA sec on enty.[Reporting SRA:ID] = sec.ID
where enty.IsActive=1 
group by sec.[Sector:Title] order by sec.[Sector:Title]




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>