0

I am creating a C# 2010 application using .NET Framework 3.5.

I have a datagridview that contains a few columns and rows [obviously]. I save this datagridview's rows in a structure of the form List<string[]>. I also have a List<double> which contains coefficients. I want to sort the structure by the coefficients using the System.LINQ library. I have attempted the following:

var linq_Query_rowrates =
    from rw in rows
    orderby matchrate descending
    select rw;

This underlines the rows in the query and shows the following error:

Error 1 Could not find an implementation of the query pattern for source type 'System.Collections.Generic.List<string[]>'. 'OrderByDescending' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?

Is it possible to sort this kind of structure using the LINQ library and if yes, how?

Note: I am aware of a lot of other methods to accomplish this, I am just interested in doing it using the LINQ library.

Note: matchrate is not a member of rows but using a member of rows does not work either.

LATER EDIT: Maybe it should be something like this?

        var linq_Query_rowrates =
            from rw in rows
            join rate in matchrate
            on matchrate equals rows
            orderby matchrate descending
            select rw;
3
  • 4
    Have you referenced 'System.Core.dll' and included a using System.Linq; directive in your source file? Commented May 30, 2012 at 13:07
  • @dtb, yes, I have included using System.Linq; and have referenced the dll. Commented May 30, 2012 at 13:10
  • How are rows and coefficients related? By index? Commented May 30, 2012 at 13:20

4 Answers 4

3

Assuming matchrate is a member of rw, you need to use the following syntax:

var linq_Query_rowrates =
    from rw in rows
    orderby rw.matchrate descending
    select rw;

Update

Ideally, you would have a navigation property for your rate relationship, so your query would look like this:

var linq_Query_rowrates =
    from rw in rows
    orderby rw.rate.matchrate descending
    select rw;

Another option is to perform a join. But joins in LINQ are ugly, and I try to avoid them.

Sign up to request clarification or add additional context in comments.

7 Comments

It is not a member but using a member of rows doesn't work either.
You can't order by something that isn't a member of rw's type.
Ok, is there any way to sort rows by matchrate using only LINQ?
Perhaps something like this might work? var linq_Query_rowrates = from rw in rows join rate in matchrate on matchrate equals rows orderby matchrate descending select rw;
Yes, you can do this with a join.
|
2

It's ugly, but it's Linq:

            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);

I haven't tested it though. It should be possible to transform it to query form.

2 Comments

I need it to sort descending actually. How would I go about doing this?
Replace OrderBy with OrderByDescending.
2

If your collection of coefficients are meant to link with your collection of string[], why do you build 2 seperate, unrelated lists? Surely it would be more robust to just build a very simple structure to hold all the information to ensure there are always appropriate coefficients for each row. It also makes sorting very straightforward.

public struct CoefficientRow
{
    public double Coefficient;
    public string[] Cells;

    public CoefficientRow(double c, string[] cells)
    {
        this.Coefficient = c;
        this.Cells = cells;
    }
}

Sorting becomes a breeze...

List<CoefficientRow> rows = new List<CoefficientRow>();
//populate the list...
var orderedRows = rows.OrderBy(cr => cr.Coefficient);
//or
var orderedRows = rows.OrderByDescending(cr => cr.Coefficient);

Inserting them to the datagridview is also still quite easy:

foreach(var row in rows)
    this.dgvDataView.Rows.Add(row.Cells);

Comments

1

If you could use .Net4, user676571's answer simplifies to:

IEnumerable<string> query = rows
  .Zip(coefficients, (r, c) => new {row = r, coef = c})
  .OrderByDescending(x => x.coef)
  .Select(x => x.row);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.