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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:27:02+00:00 2026-05-11T03:27:02+00:00

If I have a table with a title column and 3 bit columns (f1,

  • 0

If I have a table with a title column and 3 bit columns (f1, f2, f3) that contain either 1 or NULL, how would I write the LINQ to return the title with the count of each bit column that contains 1? I’m looking for the equivalent of this SQL query:

SELECT title, COUNT(f1), COUNT(f2), COUNT(f3) FROM myTable GROUP BY title 

I’m looking for the ‘best’ way to do it. The version I came up with dips into the table 4 times when you look at the underlying SQL, so it’s too slow.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:27:03+00:00Added an answer on May 11, 2026 at 3:27 am

    Here’s the solution I came up with. Note that it’s close to the solution proposed by @OdeToCode (but in VB syntax), with one major difference:

    Dim temp = _     (From t In context.MyTable _      Group t.f1, t.f2, t.f3 By t.title Into g = Group _      Select title, g).ToList  Dim results = _     From t In temp _     Select t.title, _         f1_count = t.g.Count(Function(x) If(x.f1, False)), _         f2_count = t.g.Count(Function(x) If(x.f2, False)), _         f3_count = t.g.Count(Function(x) If(x.f3, False)) 

    The first query does the grouping, but the ToList gets the grouped data as-is from the server. Eliminating the counting here keeps the resulting SQL statement from producing sub-SELECTs for each count. I do the counting in the second query locally.

    This works since I know the first query will return a manageable number of rows. If it were returning millions of rows, I’d probably have to go in another direction.

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

Sidebar

Related Questions

I have a table contain the '%' in the column title, and this cause
As title, how can I set a table's column to have the default value
Let's say I have a table tbl with columns id and title . I
i have this three tables. Table: Item Columns: ItemID, Title, Content, NoChange (Date) Table:
I have a table with contents that look similar to this: id | title
I have a table with some fixed columns id,title,created.., but there I need put
If I have Table A with columns title and description , and I want
I have created a multi column datastore on a table that allows me to
I have a table in Microsoft SQL server Management Studio with two columns title
i have table with status column CREATE TABLE movies ( id int NOT NULL

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.