Luna,
There are some errors here. The first is that you shouldn't be
returning IList<T> (you use "something" for T) from this method. You are
going to have an IEnumerable<something>, as you don't know what the query
operators are going to be.
Additionally, the "where filter" will be in the form of a Predicate<T>.
So your signature would look like this (replace T with the type of items
returned from database.Test):
public IEnumerable<T> GetSomething(int id, Predicate<T> filter)
{
// Just return the items.
return database.Test.Where(t => t.id = id).Where(filter);
}
I basically opted to use the extension methods instead of the query
syntax, they are the same thing.
A big selling point of LINQ is the ability to compose queries, and you
can do the same thing here. It might be easier to just have GetSomething
return the filter on id, and then attach the predicate later, so you would
have:
public IEnumerable<T> GetSomething(int id)
{
// Just return the items filtered on id.
return database.Test.Where(t => t.id = id);
}
And then when you call, have:
IEnumerable<T> query = GetSomething(id).Where(filter);
Of course, one could argue that your query is simple enough that you
don't really need a separate method for it, as it might convey your intent
more clearly to have the entire query defined where you want to call it
(instead of calling GetSomething, you just access database.Test).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Luna said:
How can I add additional where filters to a query via parameter input?
filter = title == "data1"
public IList<something> GetSomething(int id, Where filter)
{
var a= from t in database.Test
where t.id = id && //insert more filter query here
select t;
return a
}
Luna