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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:10:00+00:00 2026-05-16T23:10:00+00:00

Following up on a previous question, here is my general class structure.. Combine Elements

  • 0

Following up on a previous question, here is my general class structure..

Combine Elements in a List based on Type and Summate their Values, LINQ

I am trying to combine the items using their respective .Add methods, but because of Entity Framework considerations, I can only deal with base types (Not interfaces).

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

interface Item<T>
{
    T Add(T item);
}
abstract class Item
{
    public ItemType Type;
    public int Value;
}

class SubItemOne : Item, Item<SubItemOne>
{
    public int Additional { get; set; }
    public SubItemOne Add(SubItemOne item)
    {
        // ..
    }
}

class SubItemTwo : Item, Item<SubItemTwo>
{
    public int MoreData { get; set; }
    public int OtherData { get; set; }

    public SubItemTwo Add(SubItemTwo item)
    {
        // ...
    }
}

class SubItemThree : Item, Item<SubItemThree>
{
    public int StillOtherData { get; set; }
    public int SecondaryData { get; set; }
    public int TertiaryData { get; set; }

    public SubItemThree Add(SubItemThree item)
    {
        // ...
    }
}

class ItemType
{
    public string Name;
}

class Test
{
    public static void Main()
    {
        List<ItemType> types = new List<ItemType>();
        types.Add(new ItemType { Name = "Type1" });
        types.Add(new ItemType { Name = "Type2" });
        types.Add(new ItemType { Name = "Type3" });

        List<Item> items = new List<Item>();

        for (int i = 0; i < 10; i++)
        {
            items.Add(new SubItemOne
            {
                Type = types.Single(t => t.Name == "Type1"),
                Additional = i
            });
        }

        for (int i = 0; i < 10; i++)
        {
            items.Add(new SubItemTwo
            {
                Type = types.Single(t => t.Name == "Type2"),
                MoreData = 10,
                OtherData = i + 10
            });
        }

        for (int i = 0; i < 10; i++)
        {
            items.Add(new SubItemThree
            {
                Type = types.Single(t => t.Name == "Type3"),
                SecondaryData = 1000,
                StillOtherData = 1874,
                TertiaryData = i * 10
            });
        }

        List<Item> combined = new List<Item>();

        // create a list with 3 items, one of each 'type', with the sum of the total values of that type.
        // types included are not always known at runtime.

        // I am trying to invoke the .Add Method on the items so that they can be collatted together. However
        // they all return as the base type.
    }
}
  • 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-16T23:10:01+00:00Added an answer on May 16, 2026 at 11:10 pm

    The problem is that you’re going to have to deal with the interfaces at a higher level, and thus won’t be able to do so generically (i.e., using the generic type arguments). You’re going to have to define a non-generic interface (even if it’s in addition to your current generic interface) that these classes implement in order to do what you’re after. Item<SubItemOne> is no more related to Item<SubItemTwo> in terms of assignment compatibility than string is to DateTime.

    If you must do it this way, then you’ll have to use reflection. This should do the trick:

    Dictionary<ItemType, Item> sum = new Dictionary<ItemType, Item>();
    
    foreach (var item in items)
    {
        Item currSum;
    
        if (sum.TryGetValue(item.Type, out currSum))
        {
            sum[item.Type] = (Item)item.GetType().GetInterfaces().Single(
                i => i.Name == "Item").GetMethod("Add")
            .Invoke(currSum, new object[] { item });
        }
        else
        {
            sum.Add(item.Type, item);
        }
    }
    
    List<Item> combined = sum.Values.ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on a previous question I posed (see here ) and the following msdn
This is related to my previous question about selecting visible elements . Now, here's
Following my previous question ( Pylint E0202 False Positive? ) here is another one
Following on from a previous question, ( previous question here ), the problem I'm
This is a continuation of my previous question here . In the following example:
This question is based on a previous similar question. I have the following equation
Following on from a previous question I had here : Copying a string from
Following on from a previous question, shown here , I was wondering how I
Following my previous question [here][1] , I now want to calculate the number of
Following my previous question , I need to create a value consisting of a

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.