LINQ Query please help C#.Net.

Posted by Paul Matthews on Stack Overflow See other posts from Stack Overflow or by Paul Matthews
Published on 2010-03-22T17:28:12Z Indexed on 2010/03/22 17:31 UTC
Read the original article Hit count: 365

Filed under:
|
|

I'm very new to LINQ and struggling to find the answers.

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'

Due to having to encrypt all my data in the database I'm unable to do sums. Therefore I have now brought back the data and decrypt it in the application layer.

Results would be: '1' 'Approved' '10'
'3' 'Not Deceided' '6'
'2' 'Rejected' '19'
'1' 'Approved' '15'
'1' 'Approved' '5'
'3' 'Not Deceided' '6'
'1' 'Approved' '10'

using a simple class I have called back the above results, and put them in a list class.

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 at the top

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?

Also if I created a class called Statistics to hold my results how would I modify the LINQ query to give me list 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;}
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ