Thursday, 15 January 2015

C# LINQ -How to apply group by? -


I am new to linq. From the list of the following data, help me how I can implement the group and build the other to get the expected output below.

  list & lt; SalesAnalysis & gt; Analyst = new list & lt; SalesAnalysis & gt; (); AnaList.Add (New Sales Analysis ("P 2001", 200 9, 45000)); AnaList.Add (New Sales Analysis ("P 2001", 2008, 13000)); AnaList.Add (New Sales Analysis ("P 2002", 200 9, 5000)); AnaList.Add (New Sale Analysis ("P002", 2008, 15000)); AnaList.Add (New Sales Analysis ("P003", 200 9, 25000)); AnaList.Add (New Sale Analysis ("P003", 2008, 65000)); AnaList.Add (New Sales Analysis ("P004", 200 9, 5000)); AnaList.Add (New Sales Analysis ("P004", 2008, 3000)); AnaList.Add (New Sales Analysis ("P004", 2007, 95000)); AnaList.Add (New Sales Analysis ("P004", 2006, 83000)); Class sales analysis {public string ProductCode {get; Set; } Public int year {receives; Set; } Public Entry Number of Units Sold (Received; Set; } Public Sales Analysis (String Productcode, Int Year, Int NumberFunicatedWed) {ProductCode = productcode; Year = year; Number of unitsisol = no futitssold; }}  

Terms:

1) Report is required only for 2008 and 2009

2) Number = = 30000 Top movement products

3) Number => 10000 to & lt; 30000 average movement products

4) number fonts & lt; 10000 Poor Moving Products

Expected Output:

Top Movement Product Code Year Number P003 2008 65000 P001 2009 45000 Average Movement Product Code Year Number Furnace P 2003 2009 25000 P002 2008 15000 P001 2008 13000 Poor Movement Product Code Year Number Advantage P002 2009 5000 P004 2009 5000 P004 2008 3000 "post-text" itemprop = "text"> < P> To report only items for 2008 and 2009, use one block where:
  where sa.Year == 2008 || Sa.year == 2009  

For the rest, I would suggest an enema to classify "band":

  public enum movement { Top, Average, Bad} Analysis for Sale of Public Static Movement Movement (Sales Analysis) {Return sa.NumberOfUnitsSold> = 30000? protest. Fresh: Sa Number of unit sold & gt; = 10000? protest. Avg: movement. poor; }  

You can then:

  var query = to analytics in where sa.Year == 2008 || Sa.Year == 2009 Movement analysis (SA) by group; Foreign currency (different groups in query) {Console.WriteLine ("{0}:", group.Key); Foreach (different items in the group) {Console.WriteLine ("{0} / {1} / {2}", item.product code, item.year, item.NumberOfUnitsSold); } Console. Wrightite (); }  

It produces the output that you were after, modulo ordering and formatting.


No comments:

Post a Comment