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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:48+00:00 2026-05-26T17:09:48+00:00

I have a tsql snippet similar to this: select p.Make, count(p.Make) as _count from

  • 0

I have a tsql snippet similar to this:

select p.Make, count(p.Make) as _count
from Products p
where p.GroupId = @GroupId
group by p.Make

the full result set would be something with 5 rows:

Make    _count
``````````````
Make1    32
Make2    54
Make3    60
Make4    09
Make5    47

In C#, I am trying to calculate percentages of each make (assume that I know total products already) so I can construct a google image chart (pie) so I need to pass 5 values there.

I am tring to do this :

int Make1Count = from row in DataSetProducts.tables[0].AsEnumerable()
                 where row.Field<string>("Make") == MyEnum.Makers.Make1.ToString()
                 select row.Field<int>("_count");

and so on upto Make5Count. Ideally, I should have 5 results, but this is not always the case sometimes the result is like this:

Make    _count
``````````````
Make1    32 
Make4    09
Make5    47 

But I still need Make2 and 3 (they’re just zero)

My problem is, this code throws an exception when Make2 is not found, for instance.
How do I avoid this please?

note 1) In my database, there is no list of all makers so I can’t really coalesce it with anything

note 2) Here, I have avoided Convert.toInt32 just before from row in ... to keep it understandable

this should be quite trivial, but i can’t get it somehow.

  • 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-26T17:09:49+00:00Added an answer on May 26, 2026 at 5:09 pm

    Firstly, it looks like you are trying to select one single row (from your sql and linq example), but your linq returns multiple rows.. instead, try this:

    int Make1Count = (from row in DataSetProducts.tables[0].AsEnumerable()
                 where row.Field<string>("Make") == MyEnum.Makers.Make1.ToString()
                 select row.Field<int>("_count"))
                 .SingleOrDefault();
    

    That should return one row, and solve one problem.

    Now, for your real problem, when some row doesn’t exist, SingleOrDefault() will return default values for data types, check it against that. For example default value of int is 0, which is what you want, and default for bool is false etc

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

Sidebar

Related Questions

I have the following TSQL Statement: SELECT ProductId, OwnerId FROM Product How I can
Currently have this to get a value from the registry in TSQL. However, I
I have a TSQL Query that does something like this: SELECT SUM(s.Amount) as TotalSales,
I have the following TSQL query: SELECT DISTINCT MyTable1.Date FROM MyTable1 INNER JOIN MyTable2
I have a quite complex multi-join TSQL SELECT query that runs for about 8
I have to migrate this query (simplified here) from T-SQL to ORACLE SET IDENTITY_INSERT
This value (row[10]) is coming from a DataRow object, from a SQL result set
TSQL query to select all records from Customer that has an Order and also
In TSQL I could use something like Select [table] from tablename to select a
I have two similar TSQL scripts where I have SET XACT_ABORT ON. I expect

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.