2013-02-07 29 views
5

मैं निम्नलिखित बयान से मेल करने के अभिव्यक्ति के पेड़ का उपयोग कर एक गतिशील क्वेरी बनाने के प्रयास कर रहा हूँ:गतिशील प्रश्नों और अभिव्यक्ति पेड़ों के लिए विधि का चयन करें का उपयोग करना

var items = data.Where(i => i.CoverageType == 2).Select(i => i.LimitSelected); 

मैं कहाँ विधि बनाने और इसे से एक परिणाम प्राप्त कर सकते हैं; हालांकि, मैं चयन विधि नहीं बना सकता।

यहाँ मेरी जहां विधि है:

No generic method 'Select' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

:

var selectParm = Expression.Property(parm, "LimitSelected"); 
    var selectMethod = Expression.Call(
     typeof(Enumerable), 
     "Select", 
     new Type[]{typeof(BaseClassData), typeof(decimal)}, 
     whereMethod, 
     Expression.Lambda<Func<BaseClassData, decimal>>(selectParm, new ParameterExpression[]{ parm}) 

     ); 

जब मैं कोड को चलाने मैं इस त्रुटि मिलती है:

var parm = Expression.Parameter(typeof(BaseClassData), "baseCoverage"); 

var queryData = data.AsQueryable(); 

var left = Expression.Property(parm, "CoverageType"); 
var right = Expression.Constant(2m); 
var e1 = Expression.Equal(left, right); 

var whereMethod = Expression.Call(
    typeof(Queryable), 
    "Where", 
    new Type[] { queryData.ElementType }, 
    queryData.Expression, 
    Expression.Lambda<Func<BaseClassData, bool>>(e1, new ParameterExpression[] { parm })); 

यह है कि मैं क्या चयन विधि के लिए उपयोग कर रहा हूँ है

मैंने क्वेरी करने योग्य के लिए गणना करने की भी कोशिश की है और मुझे एक ही त्रुटि मिलती है।

+3

'Select' और' Where' ले ** दो ** सामान्य पैरामीटर: मैं एक स्थिर तरीका है जिसके मुझे गतिशील क्वेरी उत्पन्न करने में मदद बनाने के लिए है। – SLaks

+0

क्या आप ParameterExpression सरणी में मतलब है? मैं पूछता हूं क्योंकि कथन कोड कोड के रूप में ठीक काम करता है। अगर मैं केवल विधि का उपयोग कर क्वेरी बना देता हूं, तो सब कुछ ठीक काम करता है। यह तब होता है जब मैं अभिव्यक्ति वृक्ष में चुनिंदा विधि जोड़ने का प्रयास करता हूं। – MarkSalow

+0

मैंने selectMethod के प्रकार सरणी में टाइपऑफ (बेस क्लासडाटा) जोड़ा और यह काम करता है। – MarkSalow

उत्तर

-1

This might help with the error you describe above.

जहां/तथापि चयन आप अपनी खुद की बनाने की जरूरत नहीं है, लोगों C#/LINQ काम में बनाया गया बस अपनी स्वयं की कक्षाओं के साथ ठीक:

void Main() 
{ 
    List<testdata> data = new List<testdata>(); 
    Directory.GetFiles(@"C:\").ToList().ForEach(x=>data.Add(new testdata(){file=x,returnable=1})); 
    data.Where(x=>x.file.Contains("g")).Select(x=>x.file).Dump(); 
} 

class testdata 
{ 
    public string file {get; set;} 
    public string returnable {get; set;} 
} 
3

अभिव्यक्ति का उपयोग करने की आवश्यकता नहीं है ठीक है, आप सीधे इसके बजाय अभिव्यक्ति वृक्ष का निर्माण कर सकते हैं;

public static void Test(string[] args) { 
    using (var db = new DBContext()) { 
    //query 1 
    var query1 = db.PrizeTypes.Where(m => m.rewards == 1000).Select(t => t.name); 

    //query 2 which equal to query 1 
    Expression<Func<PrizeType, bool>> predicate1 = m => m.rewards == 1000; 
    Expression<Func<PrizeType, string>> selector1 = t => t.name; 
    var query2 = db.PrizeTypes.Where(predicate1).Select(selector1); 
    Console.WriteLine(predicate1); 
    Console.WriteLine(selector1); 
    Console.WriteLine(); 

    //query 3 which equal to query 1 and 2 
    Expression<Func<PrizeType, bool>> predicate2 = GetPredicateEqual<PrizeType>("rewards", (Int16)1000); 
    Expression<Func<PrizeType, string>> selector2 = GetSelector<PrizeType, string>("name"); 
    var query3 = db.PrizeTypes.Where(predicate2).Select(selector2); 
    Console.WriteLine(predicate2); 
    Console.WriteLine(selector2); 

    //as you can see, query 1 will equal query 2 equal query 3 
    } 
} 

public static Expression<Func<TEntity, bool>> GetPredicateEqual<TEntity>(string fieldName, object fieldValue) where TEntity : class { 
    ParameterExpression m = Expression.Parameter(typeof(TEntity), "t"); 
    var p = m.Type.GetProperty(fieldName); 
    BinaryExpression body = Expression.Equal(
    Expression.Property(m, fieldName), 
    Expression.Constant(fieldValue, p.PropertyType) 
); 
    return Expression.Lambda<Func<TEntity, bool>>(body, m); 
} 

public static Expression<Func<T, TReturn>> GetSelector<T, TReturn>(string fieldName) 
    where T : class 
    where TReturn : class { 
    var t = typeof(TReturn); 
    ParameterExpression p = Expression.Parameter(typeof(T), "t"); 
    var body = Expression.Property(p, fieldName); 
    return Expression.Lambda<Func<T, TReturn>>(body, new ParameterExpression[] { p }); 
} 
+0

चीयर्स, मायाएट ... मेरी इच्छा है कि मुझे यह पहले मिलेगा। मुझे बहुत समय बचाएगा। मैंने बस इसे कोड में कॉपी किया है। टा! – Roman

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

  • कोई संबंधित समस्या नहीं^_^