how should I make the following linq generic method returns distinct records? -


when using method below or operation, getting duplicate records. have specify custom iequalitycomparer? simple distinct() not work

internal static iqueryable<t> filterentity<t>(filters filters, iqueryable<t> entities)     {         if (filters.groupop == "and")             foreach (var rule in filters.rules)                 entities = entities.where<t>(                     rule.field, rule.data,                     (whereoperation)stringenum.parse(typeof(whereoperation), rule.op)                     );         else         {             //or             iqueryable<t> temp = (new list<t>()).asqueryable();             foreach (var rule in filters.rules)             {                 var t = entities.where<t>(                     rule.field, rule.data,                     (whereoperation)stringenum.parse(typeof(whereoperation), rule.op)                     );                      temp = temp.concat<t>(t).asqueryable();             }             entities = temp;         }         return entities;     } 

edited after suggestion @usr below - gives me correct query in sql profiler (with distinct) starts convoluted - wd cleaner solution

    internal static iqueryable<t> filterentity<t>(filters filters, iqueryable<t> entities)     {         if (filters.groupop == "and")             foreach (var rule in filters.rules)                 entities = entities.where<t>(                     rule.field, rule.data,                     (whereoperation)stringenum.parse(typeof(whereoperation), rule.op)                     );         else         {             //or             var t1 = entities.where<t>(filters.rules[0].field,filters.rules[0].data,                 (whereoperation)stringenum.parse(typeof(whereoperation),filters.rules[0].op)                 );             (int = 1; i<filters.rules.count(); i++)             {                  var t = t1.where<t>(filters.rules[i].field, filters.rules[i].data,                 (whereoperation)stringenum.parse(typeof(whereoperation), filters.rules[i].op)                 );               t1.concat<t>(t).asqueryable();             }            entities  = t1;         }         return entities.distinct<t>();     } 

this not related distinct() or iequalitycomparer. modified closure gotcha, is: loop variable rule must copied in loop body:

foreach (var rule in filters.rules) {     var rule1 = rule;     // work rule1 only. 

you can follow usr's advice doing this:

iqueryable<t> temp = null;     ....     foreach (var rule in filters.rules)     {         var rule1 = rule;         var t = entities.where<t>(rule1.field, rule1.data,             (whereoperation)stringenum.parse(typeof(whereoperation), rule1.op));              if (temp == null)                 temp = t;             else                 temp = temp.union(t); // union!!     } } return temp; 

i wonder if solves issue. note use of union (which implicit distinct). if not solve issue think there code invisible (e.g. in whereoperation) interferes.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -