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 9133289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:42+00:00 2026-06-17T08:27:42+00:00

Have got a method which returns IEnumerable<User> which I have been using Linq /

  • 0

Have got a method which returns IEnumerable<User> which I have been using Linq / Entity Framework / SQL Server to return results.

I came across a difficult conditional scenario, which was much more easily solved in C# iterating on the web server (at the end of a chain of linq statements, just before returning the data to the client):

public IEnumerable<User> ReturnUsersNotInRoles()
{
    IQueryable<User> z = (from users
                    //...many joins..conditions...
                    ).Distinct().Include(x => x.RoleUserLinks).ToList()


    IEnumerable<User> list = new List<User>();

    foreach (User user in z)
    {
        bool shouldReturnUser = true;

        foreach (var rul in user.RoleUserLinks)
        {
            if (rul.LinkStatusID == (byte)Enums.LinkStatus.Added)
                shouldReturnUser = false;
        }
        if (shouldReturnUser)
            list.Add(user);
    }

    return list;
}

Question: In C# is there a more performant / less memory overhead way of doing this?

Am only bringing back the entities I need from Linq. There is no N+1 scenario. Performance currently is excellent.

I realise that ideally I’d be writing this in SQL / Linq, as then SQL Server would do its magic and serve me the data quickly. However I’m balancing this with a potentially v.hard query to understand, and excellent performance currently with iterating, and the ease of understanding the C# way.

  • 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-17T08:27:43+00:00Added an answer on June 17, 2026 at 8:27 am

    How about this:

    public IEnumerable<User> ReturnUsersNotInRoles()
    {
        var z = (from users
                        //...many joins..conditions...
                        ).Distinct().Include(x => x.RoleUserLinks);
    
        var addedLinkStatusID = (int)Enums.LinkStatus.Added;
        return z.Where(user => 
                   false == user.RoleUserLinks.Any(link => link.LinkStatusID == addedLinkStatusID))
                .ToList();
    }
    

    This should run completely as a SQL query – you could make the first part (z) materialize by adding a .ToList() at the end of the line that defines it.


    By the way, regarding your question “In C# is there a more performant / less memory overhead way of doing this?” – well, firstly you can add a break statement right after you set shouldReturnUser = false;.

    Secondly, I prefer using the LINQ primitives whenever possible whether or not I’m working with a database:

    1. When used correctly, implementation using LINQ methods will probably be as fast or faster than anything you can write.
    2. More importantly, they promote functional, stateless programming over stateful, bug-prone programming.
    3. Also, if you are working with a database you have the bonus of being able to decide whether or not you want the code to run as a SQL query – all you have to do is decide where to materialize.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures
I have two GridViews. I've got method GetGeneralDiagnosis which returns a list of all
I have made a form validation using ajax post method. But i got the
I have a quite simple WCF service method which returns an IQueryable, just for
I've got a linq query that will return 1 result which will be an
I have the following method which is supposed to return a JSONResult so I
Dont why this is happening I have a method getOutage which returns an array
I have the following method which returns true when a PropertyInfo instance references an
I've got a method which takes a IList<Person> and returns an IEnumberable as so:
I have got a textbox on a form with a method being called from

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.