views:

29

answers:

2

I have a simple LINQ query that is going against a collection.

   Dim oList = From w In Os
                Order By w.ClassTitle 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

+1  A: 

Use delegation syntax. For example;

Dim oList = Os.Where(....).OrderBy(...)...
NetSide
While I agree that using the extension methods rather than Linq syntax makes this sort of problem clearer, I don't think you've really solved his problem.
Ryan Brunner
As the query will have to choose whether to use OrderBy or OrderByDescending this may not solve the problem.
Stuart Dunkeld
I have seen that basically he has a problem about code syntax, using this syntax, which is more clear, will make it easy to understand and he will solve it himself.
NetSide
+1  A: 

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")
Mike Fielden
that works for the ordering.. now to try to figure out NetSides way for latebinding the where
jvcoach23
Was wondering if you could explain this a bit further. in vb i need to use the olist.orderby(function(o) o.classtitle) I'm trying to google around and find out what the function part means to better understand this.. but not having much luck. would love a to better understand what it means.Thanksshannon
jvcoach23
never mind.. i think i found my answer... http://msdn.microsoft.com/en-us/library/bb534966.aspx. Type: System.Func(Of TSource, TKey)A function to extract a key from an element....
jvcoach23
Yeah that should get you want you need. The syntax does look very different in VB, I see why you were confused.
Mike Fielden