2016-08-24 74 views
3

OK設置上下文一點使用表達式樹使用這個類訪問嵌套屬性從集合中的表達

public class HomeTableInvoice { 
    public int Sys_InvoiceID { get; set; } 
    public bool Turnover { get; set; } 
    public int FK_StatusID { get; set; } 
    public string InvoiceNumber { get; set; } 
    public DateTime InvoiceDate { get; set; } 
    public string DocType { get; set; } 

    public ICollection<InvoiceCustomFields> InvoiceCustomFields { get; set; } 
} 

我設法讓一切工作我建立了一個動態的LINQ搜索子句和我用的參數是HomeTableInvoice,我可以使用

var parameter = Expression.Parameter(typeof(HomeTableInvoice), "invoice"); 
prop = Expression.Property(param, filter.SysName); 

與filter.SysName是我要過濾的字段得到一個表達式的屬性。

當試圖在底部爲ICollection構建表達式時,問題就出現了。類InvoiceCustomFields包含

public class InvoiceCustomFields : CustomFieldsBase { 
     public int? FK_SysInvoiceID { get; set; }  
     public string FK_CustomFieldHeader { get; set; }  
     public string Value { get; set; }  
    } 

我試圖訪問FkCustomFieldHeader字符串和值字符串所以,當我查詢例如條件可以像

where InvoiceNumber == 34 AndAlso (Invoice.InvoiceCustomField.FK_CustomFieldHeader == "Test" && Invoice.InvoiceCustomField.FK_CustomFieldHeader.Value == 42) 

我使用

嘗試
prop = Expression.PropertyOrField(Expression.PropertyOrField(param, "InvoiceCustomFields"), "FK_CustomFieldHeader"); 

但它拋出這個錯誤

FK_CustomFieldHeader' is not a member of type 'System.Collections.Generic.ICollection`1[APData.Audit.Entityframework.Entities.InvoiceCustomFields]' 

任何的幫助深表感謝

- 編輯 -

由伊萬試圖回答後,我得到的錯誤

No generic method 'Any' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments 

我又試圖此

prop = Expression.PropertyOrField(parameter, "InvoiceCustomFields"); 

    var queryableType = typeof(Enumerable); 
    var whereMethod = queryableType.GetMethods() 
     .First(m => { 
     var parameters = m.GetParameters().ToList();        
      return m.Name == "Any" && m.IsGenericMethodDefinition && 
               parameters.Count == 2; 
         }); 

    MethodInfo methoInfo = whereMethod.MakeGenericMethod(prop.Type); 
    var x = Expression.Call(methoInfo, Expression.PropertyOrField(parameter, "InvoiceCustomFields"), whereQuery); 

而且然後拋出

Expression of type `'System.Collections.Generic.ICollection`1[InvoiceCustomFields]' cannot be used for parameter of type 'System.Linq.IQueryable`1[System.Collections.Generic.ICollection`1[InvoiceCustomFields]]' of method 'Boolean Any[ICollection`1](System.Linq.IQueryable`1[System.Collections.Generic.ICollection`1[InvoiceCustomFields]], System.Linq.Expressions.Expression`1[System.Func`2[System.Collections.Generic.ICollection`1[.InvoiceCustomFields],System.Boolean]])` 

回答

2

讓我們來看看它是不是動態的。以下內容:

Expression<Func<HomeTableInvoice, bool>> predicate = invoice => 
    invoice.InvoiceCustomField.FK_CustomFieldHeader == "Test" && 
    invoice.InvoiceCustomField.Value == "42"; 

不是一個有效的表達式。

你真正需要做的是這樣的:

Expression<Func<HomeTableInvoice, bool>> predicate = invoice => 
    invoice.InvoiceCustomFields.Any(field => 
     field.InvoiceCustomField.FK_CustomFieldHeader == "Test" && 
     field.InvoiceCustomField.Value == "42"); 

這裏是你如何構建動態(希望你可以調整它爲您的需求與您的變量替換硬編碼的部分):

var parameter = Expression.Parameter(typeof(HomeTableInvoice), "invoice"); 

var fieldParameter = Expression.Parameter(typeof(InvoiceCustomFields), "field"); 
var anyPredicate = Expression.Lambda(
    Expression.AndAlso(
     Expression.Equal(
      Expression.PropertyOrField(fieldParameter, "FK_CustomFieldHeader"), 
      Expression.Constant("Test")), 
     Expression.Equal(
      Expression.PropertyOrField(fieldParameter, "Value"), 
      Expression.Constant("42"))), 
    fieldParameter); 
var fieldCondition = Expression.Call(
    typeof(Enumerable), "Any", new[] { fieldParameter.Type }, 
    Expression.PropertyOrField(parameter, "InvoiceCustomFields"), anyPredicate); 

// You can use the fieldCondition in your combinator, 
// the following is just to complete the example 
var predicate = Expression.Lambda<Func<HomeTableInvoice, bool>>(fieldCondition, parameter); 

// Test 
var input = new List<HomeTableInvoice> 
{ 
    new HomeTableInvoice 
    { 
     InvoiceNumber = "1", 
     InvoiceCustomFields = new List<InvoiceCustomFields> 
     { 
      new InvoiceCustomFields { FK_CustomFieldHeader = "Test", Value = "42" } 
     } 
    }, 
}.AsQueryable(); 
var output = input.Where(predicate).ToList(); 
+0

我收到錯誤沒有類型'System.Linq.Enumerable'的泛型方法'Any'與提供的類型參數和參數兼容。 –

+0

這很奇怪,因爲我已經測試過它,它的工作沒有問題(請參閱最後的測試代碼更新後的答案)。 –

+0

請參閱我的更新回答:)感謝您的幫助至此 –