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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:45:03+00:00 2026-05-24T16:45:03+00:00

I have 4 entities which I’ve defined Navigation properties on like the following: internal

  • 0

I have 4 entities which I’ve defined Navigation properties on like the following:

internal class ShipSet
{
    [Key]
    [Column("SHIPSET_ID")]
    public decimal ID { get; set; }

    public virtual ICollection<InstallLocation> InstallLocations { get; set; }
}

internal class InstallLocation
{
    [Key]
    [Column("INSTLOC_ID")]
    public decimal ID { get; set; }

    [Column("SHIPSET_ID")]
    public decimal ShipSetID { get; set; }

    public virtual ICollection<ShipSetPart> ShipSetParts { get; set; }
    public virtual ShipSet ShipSet { get; set; }
}


internal class ShipSetPart
{
    [Key]
    [Column("PARTS_ID")]
    public decimal ID { get; set; }

    [Column("INSTLOC_ID")]
    public decimal InstallLocationID { get; set; }

    public virtual CmQueueItem CmQueueItem { get; set; }
    public virtual InstallLocation InstallLocation { get; set; }
}

internal class CmQueueItem
{
    [Key]
    [Column("INVENTORY_ITEM_ID")]
    public decimal ID { get; set; }

    public virtual ICollection<ShipSetPart> ShipSetParts { get; set; }
}

I have the following fluent config:

        modelBuilder.Entity<CmQueueItem>().HasMany(p => p.ShipSetParts).
            WithRequired(s=>s.CmQueueItem).Map(m=>m.MapKey("INVENTORY_ITEM_ID"));
        modelBuilder.Entity<ShipSetPart>().HasRequired(p => p.InstallLocation);
        modelBuilder.Entity<InstallLocation>().HasRequired(p => p.ShipSet);
        modelBuilder.Entity<ShipSet>().HasRequired(p => p.Program);
        modelBuilder.Entity<CmQueueItem>().Property(p => p.LastUpdateDate).IsConcurrencyToken();

So in a nutshell, I have

ShipSet -> InstallLocation (1 to many)

InstallLocation -> ShipSetPart (1 to many)

CmQueueItem -> ShipSetPart (1 to many via INVENTORY_ITEM_ID)

I am trying to figure out how to write a LINQ query where I can create an anonymous object which includes the count of ShipSets for each CmQueueItem.

            var queueItems = from c in dbContext.CmQueueItems
                             select new
                                        {
                                            InventoryItemID = c.ID,
                                            ShipSets = 0 //[magical LINQ goes here]
                                        };

It should generate a SQL statement similar to the following:

  select d.inventory_item_id, count(a.shipset_id) as ShipSets 
  from shipsets a, 
  instlocs b, 
  ss_parts c, 
  cm_queue d
  where a.shipset_id = b.shipset_id
  and b.instloc_id = c.instloc_id
  and c.inventory_item_id = d.inventory_item_id
  group by d.inventory_item_id;

I’m new to LINQ and am having a hard time understanding how to perform aggregates and groupings like this. Any ideas?

The answer as provided below is to use the “let” keyword in LINQ:

var query = from c in dbContext.CmQueueItems
                let shipSets = (from s in c.ShipSetParts
                                select s.InstallLocation.ShipSet)
                select new
                            {
                                InventoryItemId = c.ID,
                                ShipSets = shipSets.Count(),
                            };
  • 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-24T16:45:04+00:00Added an answer on May 24, 2026 at 4:45 pm

    I haven’t got an EF model to test this against, but give this a try…

    UPDATE: Here’s how to perform the join, see if that works. Something isn’t right though because we shouldn’t have to perform the join manually, that’s what your property mapping is for.

    var queueItems = from c in dbContext.CmQueueItems
                     join s in dbContext.ShipSetParts
                     on c.ID equals s.CmQueueItem.ID
                     group s by s.CmQueueItem.ID into grouped
                     select new
                     {
                        InventoryItemID = grouped.Key,
                        //this may need a distinct before the Count
                        ShipSets = grouped.Select(g => g.InstallLocation.ShipSetID).Count()
                     };
    

    Here’s an alternate approach that’s much cleaner, but I’m unsure if it will work in EF. Give it a try and see what you think.

    var queueItems = from c in dbContext.CmQueueItems                     
                     let shipSets = (from s in c.ShipSetParts
                                     select s.InstallLocation.ShipSet)
                     select new
                     {
                         InventoryItemID = c.ID,
                         ShipSets = shipSets.Count()
                     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some entities which have decimal properties on them. These entities' properties are
I have a class which provides generic access to LINQ to SQL entities, for
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
I have an SQL query (generated by LINQ to Entities) which is roughly like
hey guys, say I have Entities and mappings like this: public class Episode {
I have two entities which are Student and Class entities. Student and Class are
I have several entities which respresent different types of users who need to be
We have bunch of Domain Entities which should be rendered to an html format,
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have two entities say Customer and Order which exist on their own and

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.