2017-03-09 8 views
5

मैं एक आगंतुक लिख रहा हूं जो IQueryable क्वेरी को बदलता है। यह Aggregate विधि null के साथ विधि का उपयोग करता है और फिर इसे बदलने के लिए कुछ func का उपयोग करता है। मेरी समस्या यह है कि यह शून्य decimal? प्रकार है। Expression.Constant(seed) जो है null और Expression.Constant प्रकार के निरंतर में बदल देती है साथक्वेरी करने योग्य। ऑग्रेगेट शून्य मानों के साथ काम नहीं कर रहा है

public static TAccumulate Aggregate<TSource,TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate,TSource,TAccumulate>> func) { 
    if (source == null) 
     throw Error.ArgumentNull("source"); 
    if (func == null) 
     throw Error.ArgumentNull("func"); 
    return source.Provider.Execute<TAccumulate>(
     Expression.Call(
      null, 
      GetMethodInfo(Queryable.Aggregate, source, seed, func), 
      new Expression[] { source.Expression, Expression.Constant(seed), Expression.Quote(func) } 
      )); 
} 

मेरे समस्या है: लेकिन मैं कुछ शोध मैंने पाया कि यह Aggregate ही जो अपनी क्वेरी को बर्बाद कर रहा है के बाद एक अपवाद

'Expression of type 'System.Object' cannot be used for parameter of type 
'System.Nullable`1[System.Decimal]' of method 'System.Nullable`1[System.Decimal] 
Aggregate[Nullable`1,Nullable`1] 
(System.Linq.IQueryable`1[System.Nullable`1[System.Decimal]], 
System.Nullable`1[System.Decimal], 
System.Linq.Expressions.Expression`1[System.Func`3[System.Nullable`1[System.Decimal], 
System.Nullable`1[System.Decimal],System.Nullable`1[System.Decimal]]])'' 

मिल ऑब्जेक्ट:

public static ConstantExpression Constant(object value) { 
    return ConstantExpression.Make(value, value == null ? typeof(object) : value.GetType()); 
} 

इस प्रकार मेरा new decimal?() बदल जाता है (object) null और मुझे यह त्रुटि मिलती है।

क्या इसके लिए कोई कामकाज है? ऐसा लगता है कि नेट फ्रेमवर्क में ठीक होना असंभव प्रतीत होता है (और यदि यह संभव है, तो इसे 4.7 या बाद में तय किया जाएगा)। मैं इसके लिए एक पुल अनुरोध बनाउंगा हालांकि मुझे यकीन है कि इसे स्वीकार नहीं किया जाएगा।

कोड स्निपेट पुन: पेश करने:

var result = new int?[] {1}.AsQueryable().Aggregate(default(int?), (a, b) => b); 
+0

उपयोग करने के लिए कस्टम सकल का उपयोग करते हुए सुझाव दे रहा हूँ एक नहीं है आपके मामले में विकल्प? – Evk

+0

मुझे लगता है कि आप 'Queryable.Aggregate' का उपयोग करने के बजाय सही 'अभिव्यक्ति' कॉन्स्टेंट 'के साथ सीधी कॉल को उत्सर्जित करने के लिए अपने आगंतुक को संशोधित कर सकते हैं। –

+0

@ user1892538 labmda में कुछ तर्क जोड़ने का प्रयास करें। आप कई कस्बों का सामना करेंगे, जो बड़े संग्रह पर थोड़े धीमे होते हैं। और वैसे भी ऐसी चीजों को करने का मूर्ख तरीका है। –

उत्तर

3

कोड स्निपेट से शुरू पुन: पेश करने

var result = new int?[] {1}.AsQueryable().Aggregate(default(int?), (a, b) => b); 

मैं

में बदल देंगे
var result2 = new int?[] {1}.AsQueryable().DefaultIfEmpty().Aggregate((a, b) => b); 

आप एक योग बराबर

खाली संग्रह के साथ चाहते हैं

var result3 = new int?[0].AsQueryable().DefaultIfEmpty().Aggregate(
    (a, b) => a.GetValueOrDefault() + b.GetValueOrDefault()); 

युक्त अशक्त

var result4 = new int?[]{1,2,null}.AsQueryable().DefaultIfEmpty().Aggregate(
(a, b) => a.GetValueOrDefault() + b.GetValueOrDefault()); 

असल में, मैं DefaultIfEmpty().Aggregate

0

इस बारे में कैसे:

public static TAccumulate Aggregate<TSource,TAccumulate>(this IQueryable<TSource> source, TAccumulate seed, Expression<Func<TAccumulate,TSource,TAccumulate>> func) { 
    if (source == null) 
     throw Error.ArgumentNull("source"); 
    if (func == null) 
     throw Error.ArgumentNull("func"); 
    return source.Provider.Execute<TAccumulate>(
     Expression.Call(
      null, 
      GetMethodInfo(Queryable.Aggregate, source, seed, func), 
      new Expression[] { source.Expression, Expression.Constant(seed, typeof(TAccumulate)), Expression.Quote(func) } 
      )); 
} 
+1

क्या आप मुझे 'System.Core.dll' संपादित करना चाहते हैं? –

+0

जाहिर है, यह बस है कि बीसीएल विधि का निश्चित संस्करण कैसा दिखना चाहिए - लेकिन आप इसे एमएस स्रोतों में ठीक नहीं कर सकते हैं। –

+0

पोस्ट में से कोई भी रिपो को छोड़कर ओपी कोड नहीं है :) –

संबंधित मुद्दे