
Untitled
By: a guest on
Jul 15th, 2012 | syntax:
None | size: 1.78 KB | hits: 11 | expires: Never
Sorting using C#'s Linq library
var linq_Query_rowrates =
from rw in rows
orderby matchrate descending
select rw;
var linq_Query_rowrates =
from rw in rows
join rate in matchrate
on matchrate equals rows
orderby matchrate descending
select rw;
List<string[]> rows = null;
List<double> coefficients = null;
rows
.Select((row, index) => new { Row = row, Index = index })
.Join(coefficients
.Select(
(coefficient, index) => new { Coefficient = coefficient, Index = index }),
x => x.Index,
x => x.Index,
(rowIndex, coefIndex) => new { Row = rowIndex.Row, Coefficient = coefIndex.Coefficient })
.OrderBy(x => x.Coefficient)
.Select(x => x.Row);
var linq_Query_rowrates =
from rw in rows
orderby rw.matchrate descending
select rw;
var linq_Query_rowrates =
from rw in rows
orderby rw.rate.matchrate descending
select rw;
public struct CoefficientRow
{
public double Coefficient;
public string[] Cells;
public CoefficientRow(double c, string[] cells)
{
this.Coefficient = c;
this.Cells = cells;
}
}
List<CoefficientRow> rows = new List<CoefficientRow>();
//populate the list...
var orderedRows = rows.OrderBy(cr => cr.Coefficient);
//or
var orderedRows = rows.OrderByDescending(cr => cr.Coefficient);
foreach(var row in rows)
this.dgvDataView.Rows.Add(row.Cells);
IEnumerable<string> query = rows
.Zip(coefficients, (r, c) => new {row = r, coef = c})
.OrderByDescending(x => x.coef)
.Select(x => x.row);