Alice
Alice

Reputation: 323

Dynamically sort with linq C#

I would like to sort a datagridview with multiple actions.

I explain myself: I've a dictionnary with a key and a value. The value is a string with ASC or DESC and I'd like to sort everything with this strategy:

string sort=String.Empty;
sort+=".OrderBy(c=>c.Value)"; ==> the first one in my list
foreach(var column in list) 
{
    if(column.Value=="DESC")
        sort+=".ThenByDescending(c=>c.Value)";

    if(column.Value=="ASC")
        sort+=".ThenByAscending(c=>c.Value)";
}

List<Formule> list=ListFormules.Where(f=>f.Identificator==null)==> It's my problem

I don't know how to change my string to work in my query. Have you an idea or a better way to do that?

Upvotes: 3

Views: 247

Answers (1)

schei1
schei1

Reputation: 2487

Post my comment as an answer due to comments.

You should check out dynamic linq.

Scott Guthrie has a detailed blog post about it. Link

Upvotes: 2

Related Questions