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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:44:26+00:00 2026-05-27T09:44:26+00:00

My first time using the Entity Framework so I’m not sure if I’m doing

  • 0

My first time using the Entity Framework so I’m not sure if I’m doing this right.

I have 4 tables:

CustomerOrder
------------
ID, StaffID, DeptID, Status, other columns...

Staff
------------
StaffID, other columns...

StaffDept
------------
StaffID, DeptID - only 2 fields

Dept
------------
DeptID, other columns...

A staff member can belong to multiple departments. The StaffDept table is just for storing this many-to-many relationship.

I need to retrieve a combination of:

  • all Customer Orders that don’t have a Status of “In Progress”
  • any extra records for the current Staff member with a Status of “In Progress”
  • any extra records with a Status of “In Progress” where the Staff member is a member of the same department as the Customer Order.

for example if I have the following data:

Staff
-----
1, Mr X, ...
2, Mr Y, ...

Dept
-----
1, Sales, ...
2, Marketing, ...

StaffDept
-----
1, 1
1, 2
2, 2

CustomerOrder
-----
1, 1, 1, In Progress, ...
2, 1, 1, Completed, ...
3, 2, 2, In Progress, ...
4, 2, NULL, In Progress, ...

I would expect if the current user was #1 they would see customer orders 1,2,3. User #2 would see 2,3,4.

Here is the code I have so far:

from co in CustomerOrders
where co.Status != "In Progress" 
  || co.StaffID == @CurrentStaffID
  || (co.StaffID != @CurrentStaffID 
      && co.DeptID!= null 
      && Staffs.Where(x => x.StaffID == @CurrentStaffID).FirstOrDefault().Depts.Any(x => x.DeptID== co.DeptID))
select new CustomerOrderObject
{
    Description = co.Description,
    Amount = co.Amount,
    ...
}

which works, but Resharper complains that the FirstOrDefault() part will throw a NULL exception at runtime. I’ve tested with a User of #3 (which doesn’t exist) in Linqpad and it doesn’t throw an error – it returns just record 2 which is what I would expect. Resharper wants me to pull the Staffs.Where(x => x.StaffID == 3).FirstOrDefault() out into a separate query run before the query above, but I’m thinking that will make it slower? I’m really not sure that any of this query is the fastest way to get the data as I’m new to linq to entities, I’ve been using linq-to-sql. Any advice would be greatly appreciated – I’m not even sure how to correctly describe the type of join I’m trying to do.

  • 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-27T09:44:27+00:00Added an answer on May 27, 2026 at 9:44 am

    Since you are simply retrieving a member of the Staff by its primary key, and you know there will be exactly one staff member by that ID, you should replace FirstOrDefault with Single.

    Staffs.Single(x => x.StaffID == @CurrentStaffID).Depts.Any(x => x.DeptID== co.DeptID))
    

    Edit 1:

    Perhaps you can fold your query a bit:

    StaffDept.Any(sd => sd.StaffId == @CurrentStaffID && sd.DeptID== co.DeptID)
    

    Edit 2:

    Staff.Any(x => x.StaffID == @CurrentStaffID && x.Depts.Any(d => d.DeptID == co.DeptID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the ADO entity framework for the first time and am not
I'm using Entity Framework 4.1 Code First. In my entity, I have three date/time
This is my first time using the Entity Framework and I'm getting some confusing
Hi I am using the ADO.NET entity framework for the first time and the
I have a small MVC 3 app using Entity Framework Code First and use
I am using Entity Framework for the first time and noticed that the entities
Using the Entity Framework 4. I have created a Code First database in the
I'm using Entity Framework for the first time, and I need to add business
using Entity Framework 4.0, it seems that the first time an operation is done
I am using the Entity framework for the first time, and would like to

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.