LINQ Query Help

good day.

I have the following columns:


i have the following values:

tim, 100, 5
dave, 175, 56
tim, 250, 76

i need help writing a LINQ query that groups the name, amount, and the number of days.  for instance, i need the following output:

tim, 0-30 days, 100
tim, 31-45, 0
tim, 45 and beyond, 76
dave, 0-30 days, 0
dave, 31 - 45, 0
dave, 45 and beyond, 175

any help would be appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Is the columns you talk about come from a MS SQL table? Or and object such as a class?
freezingHotAuthor Commented:
from a class.  thanks.
Fernando SotoRetiredCommented:
Can you post the class that you want to query so I can see how it is set up so that I can see how a query can be built for it.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Fernando SotoRetiredCommented:
Hi freezingHot;

Here is a code snippet that shows how to do what you want.

public partial class Form1 : Form
    public Form1()
    List<AccountsReceiveable> ar = new List<AccountsReceiveable>();
    private void Form1_Load(object sender, EventArgs e)
        ar.Add( new AccountsReceiveable() { Name = "Tim", Balance = 100.00M, Days = 5});
        ar.Add( new AccountsReceiveable() { Name = "Dave", Balance = 175.00M, Days = 56});
        ar.Add( new AccountsReceiveable() { Name = "Tim", Balance = 250.00M, Days = 76});
    private void button1_Click(object sender, EventArgs e)
        var balances = from a in ar
                       group a by a.Name into NameGroup
                       select new
                           Name = NameGroup.Key,
                           d30 = NameGroup.Sum(ng => (ng.Days <= 30) ? ng.Balance : 0.0M),
                           d44 = NameGroup.Sum(ng => (ng.Days > 30 && ng.Days < 45) ? ng.Balance : 0.0M),
                           d45 = NameGroup.Sum(ng => (ng.Days >= 45) ? ng.Balance : 0.0M)
        foreach (var bal in balances)
            Console.WriteLine("Name = {0} (0-30 = {1})\t(31-44 {2})\t(45 and beyond = {3})",
                bal.Name, bal.d30, bal.d44, bal.d45);
public class AccountsReceiveable
    public string Name { get; set; }
    public Decimal Balance { get; set; }
    public int Days { get; set; }

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
freezingHotAuthor Commented:

sorry that i didn't post the code as requested - i was out to lunch at the time.

i looked at your code - thank you very much.  that is exactly what i was looking for.  nicely done!
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.