I have a simple SQL query:
Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as Units
FROM tbl_Results
GROUP BY ID, OptionName
The results I got were:
'1' '4' 'Approved' '40'
'2' '1' 'Rejected' '19'
'3' '2' 'Not Decided' '12'
I have to encrypt the data in the database, and as such am unable to sum the data in relational form. To get around this I decrypt the data in the application layer, and want to use LINQ to query it there. I need the following results:
'1' 'Approved' '10'
'3' 'Not Deceided' '6'
'2' 'Rejected' '19'
'1' 'Approved' '15'
'1' 'Approved' '5'
'3' 'Not Deceided' '6'
'1' 'Approved' '10'
I put these results into class and create a strongly typed list:
public class results
{
public int ID {get;set;}
public string OptionName {get;set;}
public int Unit {get;set;}
}
I almost have the LINQ query to bring back the results like the SQL query about:
var q = from r in Results
group p.Unit by p.ID
int g
select new {ID = g.Key,
Selections = g.Count(),
Units = g.Sum()};
How do I ensure my LINQ query also give me the Option Name
?
If I created a class called Statistics
to hold my results how would I modify the LINQ query to give me List<Statistics>
result set?
public class results
{
public int ID {get;set;}
public int NumberOfSelections { get; set; }
public string OptionName {get;set;}
public int UnitTotal {get;set;}
}