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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:29:44+00:00 2026-05-14T00:29:44+00:00

I need some help adding conditions to a linq query. Take this small example

  • 0

I need some help adding conditions to a linq query.

Take this small example

  static void Main(string[] args)
    {
        List<ItemEntry> items = new List<ItemEntry>();
        items.Add(new ItemEntry(1,1,5,1));
        items.Add(new ItemEntry(2, 1, 5, 1));
        items.Add(new ItemEntry(3, 1, 5, 1));
        items.Add(new ItemEntry(4, 1, 10, 3));
        items.Add(new ItemEntry(5, 2, 5, 1));
        items.Add(new ItemEntry(6, 2, 5, 1));
        items.Add(new ItemEntry(7, 2, 5, 1));
        items.Add(new ItemEntry(8, 2, 5, 1));
        items.Add(new ItemEntry(9, 2, 5, 2));
        items.Add(new ItemEntry(10, 2, 5, 2));

        var results = from data in items
                      group data by data.ItemTypeID
                          into grouped
                          select new { ItemID = grouped.Key, ItemPrice = grouped.Sum(data => data.ItemPrice) };

        foreach (var item in results)
        {
            Console.WriteLine(item.ItemID + " " + item.ItemPrice);
        }

        Console.ReadLine();

    }
}
public class ItemEntry
{
    public int ItemID { get; set; }
    public int ItemTypeID { get; set; }
    public double ItemPrice { get; set; }
    public int ProcessedType { get; set; }

    public ItemEntry(int itemID, int itemTypeID,double itemPrice, int processedType)
    {
        ItemID = itemID;
        ItemTypeID = itemTypeID;
        ItemPrice = itemPrice;
        ProcessedType = processedType;
    }
}

I am grouping ItemTypeID then creating a sum based on the group which is fine. I now need to add an extra 2 conditions in.
I want to keep the grouping the same but I want to do the same only if ProcessedType ==1 if its types 2 or 3 then I want to total them and then minus it from the grouped total.

e.g. I will use ItemTypeID 1
If I total all of ProcessedType 1 I get 15 and if I total ProcessedType3 I have 10 so I want to return 5.

I am not sure how to add the condition into my code.

Any help would be great!!!

TIA

  • 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-14T00:29:44+00:00Added an answer on May 14, 2026 at 12:29 am

    Add a new property to your class that does the work for you.

    public class ItemEntry
    {
        // keep the existing properties
        ...
    
        public int TotalModifier
        {
            get
            {
                if (this.ProcessedType == 1)
                    return this.ItemPrice;
                else
                    return -(this.ItemPrice);
            }
        }
    

    Now sum that new property

        var results = from data in items
                      group data by data.ItemTypeID
                          into grouped
                          select new { ItemID = grouped.Key, ItemPrice = grouped.Sum(data => data.TotalModifier) };
    

    You’ll want to give this property a better name than I have, but the logic is there for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need some help regarding adding values into an array for example long[] s
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help with a query.. I have three tables. Source id name 1
I'm new to iPhone dev and need some help with adding subViews. I have
I almost finished the code, but still I need some help. This code calculates
I need some help figuring out how to do something. I got this gallery
I need some help getting my head around thread safety and dictionaries. When adding
Please i need some help in adding items to a JComboBox in Java from

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.