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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:58:03+00:00 2026-06-10T19:58:03+00:00

Below have a simple piece of code: using System; using System.Collections.Generic; using System.Linq; using

  • 0

Below have a simple piece of code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqStuff
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var aS = new List<A>();
            aS.Add(new A { aId = 0 });
            aS.Add(new A { aId = 1 });

        var bs = new List<B>();
        bs.Add(new B { aId = 0, bId = 0 });
        bs.Add(new B { aId = 0, bId = 1 });

        var cs = new List<C>();
        cs.Add(new C { bId = 0, cId = 0, Val = 100 });
        cs.Add(new C { bId = 0, cId = 1, Val = 100 });
        cs.Add(new C { bId = 1, cId = 2, Val = 100 });
        cs.Add(new C { bId = 1, cId = 3, Val = 100 });


    }
}

internal class A
{
    public int aId { get; set; }
}

internal class B
{
    public int aId { get; set; }
    public int bId { get; set; }
}

internal class C
{
    public int bId { get; set; }
    public int cId { get; set; }
    public int Val { get; set; }
}

}

What i would like to do is to have alinq that will bring me back 2 anonymous objects like this:

First: (aid = 1, Sum = 0)
Second: (aid = 0, Sum = 400)

The Sum is the sum of all objects C that are linked through B with A.

I tried with SelectMany but I never get further than getting back 5 pairs of (A, C) that i need to perform grouping on later.

E.g.

            var result = aS.SelectMany(a => bs.Where(b => b.aId == a.aId).DefaultIfEmpty(),
                                        (a, b) => new { A = a, B = b })
                            .SelectMany(b => cs.Where(c => b != null && b.B != null && c.bId == b.B.bId).DefaultIfEmpty(),
                                        (b, c) => new { A = b.A, C = c })

Something is missing, could you please help me?

Cheers.

  • 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-10T19:58:05+00:00Added an answer on June 10, 2026 at 7:58 pm

    Not the most beautiful Linq statement, but this should do it;

    var result = aS.Select(a => 
      new { a.aId, 
            sum = cs.Where(c => bs.Where(b => b.aId == a.aId)
                    .Select(b => b.bId)
                    .Contains(c.bId))
                    .Sum(c => c.Val) 
          }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have a piece of ada code shown below which is a simple switch
I have a very simple piece of code below which I think gives the
I have two OptionMenu widgets in the simple pieces of code shown below: variable
below i have a code that runs in most of my simple programs ..
I have a simple LINQ-to-Entities query as below: var BillingNumbers = from o in
Say I have a simple address class like below: public class Address { public
I have a simple piece of jquery i am using to toggle the visibility
I have a very simple little piece of Lua code, which I wrote while
I have a simple piece of code that places a background image on the

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.