SQLデータベースからテーブルを読み込むようにEntity Frameworkクラスを設定していますが、LINQ式を渡して特定のオブジェクトのみをフィルタする方法を理解できません。私は式ツリーを構築し、クラス内から動的にこれを行う方法があることを知っていますが、これを行うための最良の方法を考えることはできません。LINQ式をパラメータとして渡す
ご了承ください。
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
class MyObjectCollection<T> where T : class
{
private List<T> myInternalCollection = new List<T>();
MyObjectCollection()
{
using (var db = new MyContext())
{
foreach (T row in db.Set<T>())
{
// Enumerate the data, do whatever with it...
myInternalCollection.Add(row);
}
}
}
MyObjectCollection(var MyLinqExpression)
{
using (var db = new MyContext())
{
foreach (T row in db.Set<T>().Where.MyLinqExpression()
{
// Enumerate the data, do whatever with it...
myInternalCollection.Add(row);
}
}
}
}
// Works fine:
MyObjectCollection<Customer> allCustomers = new MyObjectCollection<Customer>();
// Would like something like this:
MyObjectCollection<Customer> customersP = new MyObjectCollection<Customer>(c => c.StartsWith("P"));
パラメータ '述語'として試してみてください。 –
Abion47
'Queryable.Where'メソッドのシグネチャを見てください(基本的には何を求めているのでしょうか)。ヒント: 'Expression>' –