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

  • Home
  • SEARCH
  • 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 995265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:41:47+00:00 2026-05-16T06:41:47+00:00

I ran across this code in one of our Entity Framework applications. I know

  • 0

I ran across this code in one of our Entity Framework applications. I know there has to be a better (more efficient) way than the three queries this code executes. Although, I can’t quite get the syntax right. (I am still learing Entity Framework myself..)

There are two tables involved. This is a simple parent/child relationship.

There is a Call table that contains information about all of our Calls and there is a Units table that contains the individual serial numbers (units) assigned to each call. Note, it isn’t a MANY-TO-MANY relationship. The Units table can/will contain duplicate records (Serial Numbers)!!

A call can have 0-Many children records in the Units table.

So, when a caller calls in, our Cust Rep enters a Serial Number (always creates a new record in the Units table), which associates it with this call. At that point we populate a “Call History” tab. This tab is built by the query below. (Search the Units table and finds all Units that match this Unit and then returns all Calls that are assigned to all of those Units (records).)

To summarize. The objective of the query is: based on the callID, find ANY other calls in the database that are also tied to ANY of the serial numbers that are assigned to this call.

Considering the Entity Framework created a Navigation in the tblCall table called “Categories” and in the tblCategory table called “Call”, there has to be a better/more efficient way to write this query. I would really like to refactor it. 🙂

Here is the existing query:

    //First, get all the Serial Numbers assigned to this Call.
    var serials = from units in context.tblUnits
                where units.callID == callID
                select units.unitSerialNumber;

    List<string> serialsList = serials.ToList<string>();

    //Get all of the Call IDs that are assigned to any of the serial numbers from the list above
    var callIDs = from units in context.tblUnits
                    where serialsList.Contains(units.unitSerialNumber)
                    select units.callID;

    List<int> callIDList = callIDs.ToList<int>();

    //Return all of the calls that are in the callID list from above
    var data = from calls in context.tblCalls
                where callIDList.Contains(calls.callID)
                select calls;

    result = data.ToList<tblCall>();

Any advice is much apprecaited!

Thanks for your help Daniel. Here is the final query:

var query = (from u1 in context.tblUnits
             join u2 in context.tblUnits on u1.unitSerialNumber equals u2.unitSerialNumber
             join c in context.tblCalls on u2.callID equals c.callID
             where u1.callID == callID
             select c).Distinct();

result = query.ToList();
  • 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-16T06:41:48+00:00Added an answer on May 16, 2026 at 6:41 am

    I think you could replace it with a query similar to this:

    var query = from u1 in context.tblUnits
            join u2 in context.tblUnits on u1.unitSerialNumber equals u2.unitSerialNumber
            join c in context.tblCalls on (u2.callID ?? -1) equals c.callID
            where u1.callID == callID
            select c;
    
    var result = query.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.