Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:07:53+00:00 2026-06-05T11:07:53+00:00

when using the method below for an OR operation, I am getting back duplicate

  • 0

when using the method below for an OR operation, I am getting back duplicate records. do I have to specify a custom IEqualityComparer? a simple distinct() does 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 FROM @usr below – This gives me the correct query in sql profiler (with a distinct) but this starts to look too convoluted – I wd like a 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)
                );
            for (int i = 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>();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:07:55+00:00Added an answer on June 5, 2026 at 11:07 am

    This is not related to Distinct() or IEqualityComparer. It is the modified closure gotcha, that is: the loop variable rule must be copied in the loop body:

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

    You can follow Usr’s advice by 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 it solves your issue. Note the use of Union (which is an implicit Distinct). If it does not solve your issue I think there is some code invisible to us (e.g. in your WhereOperation) that interferes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with android 4.0.3. I'm using the method below to get local
Possible Duplicate: Cross-thread operation not valid I have a problem with using of threading
I'm using the below method to reply to mails coming in to a business
I'm having a problem de-compressing a ZIP file using Java. The method is below.
I am using the code below, its basically babylonian method, how can i modify
As I created a Progress bar using below code in a on click method
I'm using OpenRasta framework in a .net service and I've a method as below
Using Ember 0.9.7.1 Getting this error in IE8 and below when my app starts
When programming against a fluent API or just using method-chaining, I've seen the style
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.