2013-04-18 31 views
1

爲了簡便起見,假設我有一個名爲參與者模型像如何根據模型的屬性進行查詢?

public class Participant() 
{ 
    public int? ID {get; set;} 
    public string Name {get; set;} 
    public DateTime JoinDate {get; set;} 
    public string Address1 {get; set;} 
    public string City {get; set;} 
    public string County {get; set;} 
} 

public IList<Participant> SearchParticipants(Participant objParticipant) 
{ 
    using (Db.Context()) 
    { 
     //HOW CAN I ACHEIVE THIS? USING EF 
     //WARNING PSEUDO-CODE/MAGIC FUNCTION (SearchMatches) BELOW 

     return Db.Entities<Participant>().SearchMatches(objParticipant); 
    } 
} 

基本上,我並不想構建多個.where(k => k.PropertyName)查詢。我認爲某些PHP MVC框架具有這種功能,傳遞一個填充了某些屬性的對象,並從數據庫中獲取匹配結果的數組(在我們的例子中爲IList)。

+0

你不會需要多個。哪裏查詢,只有一個在其中指定您需要匹配哪些屬性,例如.Where(k => k.Prop1 == given.Prop1 && k.Prop2 == given.Prop2 ..)。你也可以把它放在一個委託中,並把它作爲參數發送。 –

+0

可能是反射和動態linq的組合。你會遍歷你的對象的屬性不是null(或者一些「忽略」值),不斷地使用動態linq添加where子句。 –

+0

你打算指出哪些字段要忽略以及使用哪些字段,因爲你有不可空字段? –

回答

0

您可以使用反射的組合來遍歷您的屬性,並使用dynamic linq來構建Where謂詞。

下面是一個簡單的例子,作爲一個起點/概念驗證。

目前,它採用null作爲值,表明我們不想where子句,但如果你有一個值類型屬性(如int),那麼你可以添加屬性像[FilterIgnore]或東西,檢查是否能循環通過屬性。

創建一個新的控制檯應用程序以及與此替換的Program.cs然後從樣品添加到項目動態LINQ文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Linq.Dynamic; 

class MyObject 
{ 
    // Some properties 
    public string PropertyA { get; set; } 
    public string PropertyB { get; set; } 
    public DateTime? PropertyC { get; set; } 

    static void Main(string[] args) 
    { 
     // Our repository 
     var list = new List<MyObject>() { 
       new MyObject() { PropertyA = "test"} , 
       new MyObject() { PropertyB = "test"} , 
       new MyObject() { PropertyC = DateTime.Today} , 
       new MyObject() { PropertyC = DateTime.Today, 
           PropertyA = "test"} 
      }; 

     // Loop through the filtered results 
     // (calling our GetByExample method with a 
     // new object with a property set) 
     foreach (var obj in 
       GetByExample(list, 
           new MyObject() { PropertyC = DateTime.Today })) 
     { 
      // Write some output so we can see it working 
      Console.WriteLine("Found object"); 
      Console.WriteLine(obj.PropertyA); 
      Console.WriteLine(obj.PropertyB); 
      Console.WriteLine(obj.PropertyC); 
     } 

     // Wait for the user to press a key before we close 
     Console.ReadKey(); 
    } 

    static IQueryable<MyObject> GetByExample(List<MyObject> objects, 
              MyObject filterObj) 
    { 
     // Create our query variable that we'll add to 
     var filteredObjects = objects.AsQueryable(); 

     // Loop through each property on this object 
     foreach (var prop in filterObj.GetType().GetProperties()) 
     { 
      // Get the value of this property 
      var propVal = prop.GetValue(filterObj, null); 

      if (propVal != null) 
      { 
       // If the property isn't null add a where clause 
       filteredObjects = 
        filteredObjects.Where(string.Format("{0} = @0", 
                 prop.Name), 
              propVal); 
      } 
     } 

     return filteredObjects; 
    } 
} 
+0

編譯錯誤'filteredObjects = filterObjects.Where(string.Format(「{0} = @ 0」,prop.Name),propVal);'並且不知道如何糾正它 –

+0

確保您下載並添加動態linq到你的項目。 –

+0

謝謝喬治!動態Linq肯定似乎在這裏改變遊戲規則。可惜這還沒有被納入E.F 5.0(或者它是否已經?) –

相關問題