late binding in linq
I have a simple LINQ query that is going against a collection.
Dim oList = From w In Os
Order By w.ClassTitl开发者_运维百科e Descending
Select w
What I'd like to be able to do though is pass into this whether it's descending or ascending. I'm not sure how to do that.
Also, if I were to have a where clause in here.. say
where w.ClassTitle = "Test"
How can I make the "Test so that I can pass it into the LINQ query.
Thanks shannon
I dont think you can pass that "into" this query, however you could do the regular query
var oList = from w in Os
select w
Then when the user takes some action you could simply do an order by after that fact.
oList.OrderBy(o => o.ClassTitle)
or
oList.OrderByDescending(o => o.ClassTitle)
UPDATE:
As far as the late binding, what you could do is write a method that would execute the Where clause. Perhaps using an extension method might work. I'm more familiar with C# so my syntax might be a bit off
public static IEnumerable<Os> ExecuteWhere (this Table<Os> table, Expression<Func<Os, bool>> predicate)
{
return table.AsQueryable<Os>().Where(predicate);
}
Then to call it like so:
oList.ExecuteWhere(a => a.ClassTitle == "Test")
Use delegation syntax. For example;
Dim oList = Os.Where(....).OrderBy(...)...
精彩评论