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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:04+00:00 2026-05-13T19:59:04+00:00

I can do this by looping the result of a simplified query: var query

  • 0

I can do this by looping the result of a simplified query:

var query = from myrecord in dt.AsEnumerable()
    where myrecord.Field<string>("field1") == "value1" || myrecord.Field<string>("field1") == "value2"
    select myrecord;

foreach(var myrecord in query)
{
    //if value1, then "X"
    //sum += field2
}

But, I want to know if it’s possible within the LINQ statement.

Anonymous class with two members: Name and Value. Name is “X” or “Y” depending on field1 and Value is the sum of all field2 values for records where the conditions are met. I think I need to use the Count() method, but I’m not sure how or where. Maybe I need to use “group” and “into” to get the count from a temporary table?

If there are records with (field1 == “value1”), the string will be “X” else the string will be “Y”.

var query = from table in dt.AsEnumerable()
    where table.Field<string>("field1") == "value1" || 
        table.Field<string>("field1") == "value2"
    select new
    {
        Name = (condition ? "X" : "Y"),
        Value = //sum all field2 values
    };

Thanks in advance!

  • 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-13T19:59:04+00:00Added an answer on May 13, 2026 at 7:59 pm

    sure you can, Following sample works similarly

    class Program
        {
            static void Main(string[] args)
            {
                List<Person> persons= new List<Person>();
                var result = from p in persons
                select new
                 {
                  Name = (s.IsMarried ? s.X : s.Y)
                 };
            }
        }
        class Person
        {
            public bool IsMarried { get; set; }
            public string X { get; set; }
            public string Y { get; set; }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
I've been requesting MySQL results and looping through them like this: $query = SELECT
I have javascript function return array like this: <script type=text/javascript> function looping() { var
Most looping code looks like this retVal=NULL for i { for j { result
I have a routine that examines thousands of records looking for discrepancies. This can
Looking at this jQuery example, how can I modify the code so that it
Thanks for looking at this question. I wanted to know how can I use
any help you can give is very gratefully accepted. I've been looking at this
I'm looking for something similar to this: Active navigation with jQuery - can't apply
I'm looking to create a document like this: <root/> That I can add 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.