0

With my following code:

using System.Collections.Generic;
using System.Linq;

namespace SampleGrouping
{
internal class Program
{
    private static void Main(string[] args)
    {
        var sample = new List<Samples>
                                   {
                                       new Samples{ParameterID = 11,MaterialID = 855,ProductID   =  955,Quantity = 100},
                                       new Samples{ParameterID = 11,MaterialID = 855,ProductID   =  955,Quantity = 200},
                                       new Samples{ParameterID = 12,MaterialID = 856,ProductID   =  956,Quantity = 100},
                                       new Samples{ParameterID = 12,MaterialID = 856,ProductID   =  956,Quantity = 400}
                                   };
        // Result: Groupby ParameterID, MaterialID, ProductID
        // ParameterID = 11,MaterialID = 855,ProductID = 955,Quantity = 300
        // ParameterID = 12,MaterialID = 856,ProductID = 956,Quantity = 500

        var enumerable = from s in sample
                         group sample by new
                                             {
                                                 s.ParameterID,
                                                 s.MaterialID,
                                                 s.ProductID,

                                             };
    }
}

internal class Samples
{
    public int MaterialID { get; set; }
    public int ParameterID { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
}
}

How can I achieve result as : List as

ParameterID = 11,MaterialID = 855,ProductID = 955,Quantity = 300
ParameterID = 12,MaterialID = 856,ProductID = 956,Quantity = 500

Thanks

1 Answer 1

3

Group by "s", instead of "sample". Then you can select the data as you need it:

var enumerable =
    from s in sample
    group s by new
    {
        s.ParameterID,
        s.MaterialID,
        s.ProductID,
    } into GroupedSample
    select new
    {
        GroupedSample.Key.ParameterID,
        GroupedSample.Key.MaterialID,
        GroupedSample.Key.ProductID,
        TotalQuantity = GroupedSample.Sum(gs => gs.Quantity)
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.