Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.78 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sorting using C#'s Linq library
  2. var linq_Query_rowrates =
  3.     from rw in rows
  4.     orderby matchrate descending
  5.     select rw;
  6.        
  7. var linq_Query_rowrates =
  8.             from rw in rows
  9.             join rate in matchrate
  10.             on matchrate equals rows
  11.             orderby matchrate descending
  12.             select rw;
  13.        
  14. List<string[]> rows = null;
  15.             List<double> coefficients = null;
  16.  
  17.             rows
  18.                 .Select((row, index) => new { Row = row, Index = index })
  19.                 .Join(coefficients
  20.                             .Select(
  21.                                 (coefficient, index) => new { Coefficient = coefficient, Index = index }),
  22.                                 x => x.Index,
  23.                                 x => x.Index,
  24.                                 (rowIndex, coefIndex) => new { Row = rowIndex.Row, Coefficient = coefIndex.Coefficient })
  25.                 .OrderBy(x => x.Coefficient)
  26.                 .Select(x => x.Row);
  27.        
  28. var linq_Query_rowrates =
  29.     from rw in rows
  30.     orderby rw.matchrate descending
  31.     select rw;
  32.        
  33. var linq_Query_rowrates =
  34.     from rw in rows
  35.     orderby rw.rate.matchrate descending
  36.     select rw;
  37.        
  38. public struct CoefficientRow
  39. {
  40.     public double Coefficient;
  41.     public string[] Cells;
  42.  
  43.     public CoefficientRow(double c, string[] cells)
  44.     {
  45.         this.Coefficient = c;
  46.         this.Cells = cells;
  47.     }
  48. }
  49.        
  50. List<CoefficientRow> rows = new List<CoefficientRow>();
  51. //populate the list...
  52. var orderedRows = rows.OrderBy(cr => cr.Coefficient);
  53. //or
  54. var orderedRows = rows.OrderByDescending(cr => cr.Coefficient);
  55.        
  56. foreach(var row in rows)
  57.     this.dgvDataView.Rows.Add(row.Cells);
  58.        
  59. IEnumerable<string> query = rows
  60.   .Zip(coefficients, (r, c) => new {row = r, coef = c})
  61.   .OrderByDescending(x => x.coef)
  62.   .Select(x => x.row);