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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:00:45+00:00 2026-05-13T18:00:45+00:00

Working with four tables. Users -> has basic user info including a userid and

  • 0

Working with four tables.

Users -> has basic user info including a userid and a departmentid (int)
Groups -> basic group info including a groupid
GroupsMembers -> table that has the relationship between a group and it’s members, many to many relationship, so groupid and userid are the columns
Departments -> basic department info including deptid

I have a fk from the departmentid in the users table to the deparmtnet id in the departments table.

FK from groups groupid to groupsmembers groupid
FK from users userid to groupsmembers userid

This allows the groups in the edmx to have a users navigation property which will have all the members of the group.

var grp = grpSource.FirstOrDefault(g => g.GroupID == groupID)
if (grp.GroupID > 0)
{
    var userQuery = from u in grp.Users
                    where !u.Deleted
                    select u;
    userQuery = userQuery.OrderBy(u => u.Department.Name);
}

I am including Users.Department.

The problem comes because users do not have to have a department, so the departmentid column is nullable. If there are any users for which the departmentid is null, the orderby breaks and says u.Department is null. If no departmentids are null, it works great. I need a way to sort based on Department.Name even if there are null departmentids. Any suggestions?

  • 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-05-13T18:00:45+00:00Added an answer on May 13, 2026 at 6:00 pm

    You can use the conditional operator to check if the department is null :

    userQuery = userQuery.OrderBy(u => (u.Department != null) ? u.Department.Name : String.Empty);
    

    For improved clarity, I created the following extension method :

        public static TResult IfNotNull<TSource, TResult>(this TSource obj, Func<TSource, TResult> selector, TResult defaultValue)
        {
            if (obj != null)
                return selector(obj);
            return defaultValue;
        }
    

    It can be used as follows :

    userQuery = userQuery.OrderBy(u => u.Department.IfNotNull(d => d.Name, String.Empty));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on four of CL and RLG programs. The call stack
In school I am part of a team of four working to create a
I'm working on problem four of Project Euler and am running into a stackoverflow
We have situation where say we have four engineers that are working on software
I'm working with Jquery in Drupal. I have sets of four blocks that all
I am working on a interface in java swing.we have four system connected with
I am developing an intranet web application. I am working now on the User
I am working on an application which uses bump technology.I do have four tab
I have been working on a website that has to do with a subscription
I am Working on simple login application in which user is first shown login

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.