What you want to do is this:
IList <MyClass> myList2 = myRepository.GetBy (x => x.Name == "SomeName" && x.ID
= 5);
It is true that you can represent x => x.Name == "SomeName" && x.ID = 5 with Expression <Func <T, bool >>
but also what you can do with the delegate Func <T, bool>
only.
Regardless of which were to take the data will always be from an IEnumerable <T>
so you always will have the Where
method (whenever you use the namespace System.Linq
), which accepts as a parameter a delegate Func <T, bool>
. If the object IEnumerable <T>
is a DbSet <T>
, this will take care of transforming the delegate Func <T, bool>
in a sql query. Remember that a Linq query, which is what is being used, is only executed when the query data is used or agrees with methods ToList ()
or ToArray ()
for example.
example:
IEnumerable <MyClass> list = ...
from wherever you get the data even from DbSet of EntityFramework
var query = list.Where (x => x.Name == "SomeName" && x.ID = 5);
query is a shost a query, it contains no data until this is done
foreach (var x in list) is being consumed, so the query is executed
{
var c = x.Name;
}
or this
`var temp = query.ToList ();`
This force to stored in a List <MyClass>
With all this I want to say that if you use the DbSet of EntityFramework, what happens is that the delegate Func <T, bool>
is transformed into a sql query, so that the data manager is responsible for filtering data (as it should be).
From this alone you would have to simply have your method
public IList <T> GetBy (Func <T, bool> expression)
{
origen.Where (expression).ToList();
}