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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:40:12+00:00 2026-05-13T20:40:12+00:00

I have a LINQ question. Let’s say I have a database with the following

  • 0

I have a LINQ question. Let’s say I have a database with the following values:

===================
Date,product,orders
-------------------
1/2/2003,apple,3
1/2/2003,orange,5
1/3/2003,orange,6
1/4/2003,grape,2
===================

How can I use LINQ to get a result that groups the items by date and puts them into a variable that looks like this:

==========================
Date, Apple, Orange, Grape
--------------------------
1/2/2003,3,5,0
1/3/2003,0,6,0
1/3/2003,0 0,2
==========================

Any help would be much appreciated. Thanks!

  • 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-13T20:40:13+00:00Added an answer on May 13, 2026 at 8:40 pm

    Here the test I done:

    var data = new []{
        new { Date = new DateTime(2003, 1, 2), Product = "apple", Orders = (int?) 3 },
        new { Date = new DateTime(2003, 1, 3),Product =  "orange", Orders = (int?) 5 },
        new { Date = new DateTime(2003, 1, 4), Product = "grape", Orders = (int?) 2 },
        new { Date = new DateTime(2003, 1, 4), Product = "grape", Orders = (int?) null }
    };
    
    var result = data.GroupBy(x => x.Date)
                     .Select(g => new {
                       Date = g.Key,
                       Apples = g.Where(x => x.Product == "apple").Sum(x => x.Orders),
                       Oranges = g.Where(x => x.Product == "orange").Sum(x => x.Orders),
                       Grapes = g.Where(x => x.Product == "grape").Sum(x => x.Orders)
                     });
    

    The result for the test data does not contain nulls, but 0 for Apples, Oranges, and Grapes.

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

Sidebar

Related Questions

Referring to this question, let's say we have the following scenario - A model
Let say I have the following desire, to simplify the IConvertible's to allow me
I am learning LINQ and have a very simple question that I think will
I have a linq to sql database. Very simplified we have 3 tables, Projects
Following on from the excellent answer to my previous question: Linq Entity Framework generic
First let me start by explaining my use case: Say there is a database
I have the following linq statement builtIn = (from type in Assembly.GetExecutingAssembly().GetTypes() where type.IsClass
Explanation: Let's say I have an object graph that's nested several levels deep and
I have LINQ statement that looks like this: return ( from c in customers
What I have: LINQ to SQL auto generated class, which corresponds to the shape

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.