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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:07:41+00:00 2026-06-12T03:07:41+00:00

I’m looking to fill an object model with the count of a linq-to-sql query

  • 0

I’m looking to fill an object model with the count of a linq-to-sql query that groups by its key.

The object model looks somewhat like this:

public class MyCountModel()
{
  int CountSomeByte1 { get; set; }
  int CountSomeByte2 { get; set; }
  int CountSomeByte3 { get; set; }
  int CountSomeByte4 { get; set; }
  int CountSomeByte5 { get; set; }
  int CountSomeByte6 { get; set; }
}

This is what I have for the query:

var TheQuery = from x in MyDC.TheTable
               where ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7
               group x by x.SomeByte into TheCount
               select new MyCountModel()
               {
                   CountSomeByte1 = TheCount.Where(TheCount => TheCount.Key == 1)
                                            .Select(TheCount).Count(),

                   CountSomeByte2 = TheCount.Where(TheCount => TheCount.Key == 2)
                                            .Select(TheCount).Count(),     
                   .....

                   CountSomeByte6 = TheCount.Where(TheCount => TheCount.Key == 6)
                                            .Select(TheCount).Count(), 

               }.Single();

ListOfRecordIDs is list of longs that’s passed in as a parameter. All the CountSomeByteN are underlined red. How do you do a count of grouped elements with the group’s key mapped to an object model?

Thanks for your suggestions.

  • 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-12T03:07:43+00:00Added an answer on June 12, 2026 at 3:07 am

    The select is taking each element of your group and projecting them to identical newly created MyCountModels, and you’re only using one of them. Here’s how I’d do it:

    var dict = MyDC.TheTable
        .Where(x => ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7)
        .GroupBy(x => x.SomeByte)
        .ToDictionary(grp => grp.Key, grp => grp.Count());
    
    var result = new MyCountModel()
    {
        CountSomeByte1 = dict[1];
        CountSomeByte2 = dict[2];
        CountSomeByte3 = dict[3];
        CountSomeByte4 = dict[4];
        CountSomeByte5 = dict[5];
        CountSomeByte6 = dict[6];
    }
    

    EDIT: Here’s one way to do it in one statement. It uses an extension method called Into, which basically works as x.Into(f) == f(x). In this context, it can be viewed as like a Select that works on the whole enumerable rather than on its members. I find it handy for eliminating temporary variables in this sort of situation, and if I were to write this in one statement, it’s probably how I’d do it:

    public static U Into<T, U>(this T self, Func<T, U> func)
    {
        return func(self);
    }
    
    var result = MyDC.TheTable
        .Where(x => ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7)
        .GroupBy(x => x.SomeByte)
        .ToDictionary(grp => grp.Key, grp => grp.Count())
        .Into(dict => new MyCountModel()
        {
            CountSomeByte1 = dict[1];
            CountSomeByte2 = dict[2];
            CountSomeByte3 = dict[3];
            CountSomeByte4 = dict[4];
            CountSomeByte5 = dict[5];
            CountSomeByte6 = dict[6];
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.