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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:58:53+00:00 2026-05-29T23:58:53+00:00

I have the following data structure: public class Difference { public Difference() { }

  • 0

I have the following data structure:

public class Difference
{
    public Difference() { }
    public Differential Type { get; set; }
    public int Magnitude { get; set; }
}
public enum Differential
{
    low,
    middle,
    high
}

I may have the following data:

Magnitude      Type
4456           low
4456           low
4423           low
4421           low
1000           high
1000           high
1001           high
1560           middle
4456           low
4421           low

Im trying to return a dataset that gives me the Count on the number identical maginitudes and their types BUT ONLY THE TOP IN EACH TYPE in descending order of count so for the above (in order)

Value: 4456 Type: Low Count: 3

Value: 1000 Type: High Count: 2

Value: 1560 Type: Middle Count: 1

Notice how 4421 at a count of 2 wasn’t in there because 4456 had a larger count?

i think i have got close but not quite there:

var query = (from value in differences
             group value by new {value.Magnitude, value.Type} into groupjoin
             select new
             {
                 Value = groupjoin.Key.Magnitude,
                 Type = groupjoin.Key.Type,
                 Count = groupjoin.Count()                                         
             })
            .OrderByDescending(v => v.Count)
            .ThenByDescending(v => v.Value).ThenByDescending(v => v.Type).ToList();

UPDATE

Based on what was suggested below by Douglas ive managed to implement it with the following. Im not too concerned though if there are ties, i would like to return just the First() one (for each type) if possible. i can only do this by looping:

var query = (from value in differences
                                 /*where value.type == Differential.low*/
                                 group value by new {value.Magnitude, value.Type} into groupjoin
                                 select new
                                 {
                                     Value = groupjoin.Key.Magnitude,
                                     Type = groupjoin.Key.Type,
                                     Count = groupjoin.Count()                                         
                                 });

                    var query2 = query.Where(g1 => !query.Any(g2 =>
                                    g2.Type == g1.Type &&
                                    g2.Count > g1.Count));

                    int highest = 0;
                    int lowest = 0;
                    int middle = 0;
                    foreach (var item in query2)
                    {
                        if (item.Type == Differential.high && item.Count > highest) 
                        {
                            oresult.numberOfHighHits = item.Count;
                            oresult.highTimeMagnitude = item.Value;
                        }
                        if (item.Type == Differential.low && item.Count > lowest)
                        {
                            oresult.numberOfLowHits = item.Count;
                            oresult.lowTimeMagnitude = item.Value;
                        }
                        if (item.Type == Differential.middle && item.Count > middle)
                        {
                            oresult.numberOfMiddleHits = item.Count;
                            oresult.middleTimeMagnitude = item.Value;
                        }
                    }
  • 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-29T23:58:54+00:00Added an answer on May 29, 2026 at 11:58 pm

    I’m preserving the first part of your LINQ query that gives the groupings of distinct magnitude–type pairs (and their counts):

    var query = 
        from value in differences                                     
        group value by new {value.Magnitude, value.Type} into groupjoin
        select new
        {
            Value = groupjoin.Key.Magnitude,
            Type = groupjoin.Key.Type,
            Count = groupjoin.Count()                                         
        };
    

    Then, I’m writing another LINQ which takes all the groupings produced by the first query and, for each grouping g1, checks that there does not exist any other grouping g2 which has the same type and a larger count. This would ensure that only the groupings with the largest count of their type are returned. In the case of ties (multiple groupings of the same type have the same largest count), all top groupings are returned.

    var query2 = query.Where(g1 => 
        !query.Any(g2 => 
            g2.Type == g1.Type && 
            g2.Count > g1.Count));
    

    Edit: To return a single result for each type even in the case of ties, we could add another condition to our query2 filter such that, if two groupings have the same Type and the same Count, then the one with the larger Value (originally named Magnitude) is picked. If you would prefer to pick the one with the smaller Value, just replace the last > with a <.

    This approach would make the results of your query deterministic (rather than arbitrarily dependent on, for example, which grouping occurs first in the original list).

    var query2 = query.Where(g1 => 
        !query.Any(g2 => 
            g2.Type == g1.Type && 
            (g2.Count > g1.Count || (g2.Count == g1.Count && g2.Value > g1.Value))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data structure: public class Person { public string Name{get;set;} public
Suppose I have the following class structure: public class State { public int Id
I have the following data structure: [DataServiceEntity] public class User { public string RowKey
I have a structure that looks following Class TreeNode { public TreeNode Parent {
I have the following Object: public class Constraint { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) int id; String
Let's say I have the following class structure: class Car; class FooCar : public
I have a class structure in C#, similar to the following: [DataContract] class Data
I have the following data structure (a list of lists) [ ['4', '21', '1',
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have an odd linq subquery issue. Given the following data structure: Parents Children

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.