none
How to extract first data from list with some condition using LINQ RRS feed

  • Question

  • see this is my data

    List<WeightageRowNumber> oWeightageRowNumber = new List<WeightageRowNumber>
                {
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="BW",Weightage=1,RowNumber=1},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="3P-1",Weightage=1,RowNumber=2},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=2,RowNumber=3},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=3,RowNumber=4},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Net Revenue",Broker="",Weightage=3,RowNumber=5},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="BW",Weightage=1,RowNumber=6},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="3P-1",Weightage=1,RowNumber=7},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=2,RowNumber=8},
    
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=3,RowNumber=9},
                    new WeightageRowNumber { Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="",Weightage=3,RowNumber=10},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="BW",Weightage=1,RowNumber=11},
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="3P-1",Weightage=1,RowNumber=12},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=2,RowNumber=13},
    
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=3,RowNumber=14},
                    new WeightageRowNumber { Section = "Key Financials", Lineitem = "Quick Ratio",Broker="",Weightage=3,RowNumber=15},
    
                };
    
                

    i want to extract first data from above list whose Weightage is 1.

    var _data = oWeightageRowNumber.Where(x => x.Weightage == 1).FirstOrDefault();

    the above code is not give right data.

    output should return these data

    Section = "Consensus Model", Lineitem = "Net Revenue",Broker="BW",Weightage=1,RowNumber=1
    
    Section = "Consensus Model", Lineitem = "Cost of Revenue",Broker="BW",Weightage=1,RowNumber=6
    
    Section = "Key Financials", Lineitem = "Quick Ratio",Broker="BW",Weightage=1,RowNumber=11

    the above 3 records will be my output whose weitage is one. so tell me what will be my LINQ code. thanks

    Saturday, February 9, 2019 11:35 AM

Answers

  • i solved it

                var _data1 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Select(group =>
                    new
                    {
                        Section = group.Key.Section,
                        Lineitem = group.Key.Lineitem,
                        Weightage = group.Key.Weightage,
                        //Result = group.ToList()
                        RowNumber=group.FirstOrDefault().RowNumber
    
                    }).ToList().Where(z => z.Weightage == 1);

    • Marked as answer by Sudip_inn Saturday, February 9, 2019 4:04 PM
    Saturday, February 9, 2019 12:32 PM

All replies

  • Hello,

    For returning the first item that meets the condition you should use

    var _data = oWeightageRowNumber.FirstOrDefault(x => x.Weightage == 1);
    The Where is not needed as in you code presented.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 9, 2019 12:11 PM
    Moderator
  • NO. i want to return first data whose weitage is one. FirstOrDefault will return first one from the collection.

    Saturday, February 9, 2019 12:14 PM
  • I tried this one which works

                var _data1 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Select(group =>
                    new
                    {
                        Section = group.Key.Section,
                        Lineitem = group.Key.Lineitem,
                        Weightage = group.Key.Weightage,
                        Result = group.ToList()
    
                    }).ToList().Where(z => z.Weightage == 1);


    when i try to simplify the above query with below query then it did not return expected data

                var _data2 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Where(y => y.Key.Weightage == 1)
                    .SelectMany(group => group.Select(data =>
                    new
                    {
                        Section = data.Section,
                        Lineitem = data.Lineitem,
                        Weightage = data.Weightage,
                        RowNumber = data.RowNumber
                    })).ToList();
    

    Tell me why my second query did not return desirable output?

    thanks

    Saturday, February 9, 2019 12:16 PM
  • NO. i want to return first data whose weitage is one. FirstOrDefault will return first one from the collection.

    That does not make any sense, 

    Here is what I see

    var _data1 = oWeightageRowNumber.Where(x => x.Weightage == 1);
    var _data2 = oWeightageRowNumber.FirstOrDefault(x => x.Weightage == 1);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 9, 2019 12:25 PM
    Moderator
  • I tried this one which works

                var _data1 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Select(group =>
                    new
                    {
                        Section = group.Key.Section,
                        Lineitem = group.Key.Lineitem,
                        Weightage = group.Key.Weightage,
                        Result = group.ToList()
    
                    }).ToList().Where(z => z.Weightage == 1);


    when i try to simplify the above query with below query then it did not return expected data

                var _data2 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Where(y => y.Key.Weightage == 1)
                    .SelectMany(group => group.Select(data =>
                    new
                    {
                        Section = data.Section,
                        Lineitem = data.Lineitem,
                        Weightage = data.Weightage,
                        RowNumber = data.RowNumber
                    })).ToList();

    Tell me why my second query did not return desirable output?

    thanks

    Tell me what is wrong with this?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 9, 2019 12:27 PM
    Moderator
  • i solved it

                var _data1 = oWeightageRowNumber.GroupBy(x => new { x.Section, x.Lineitem, x.Weightage })
                    .Select(group =>
                    new
                    {
                        Section = group.Key.Section,
                        Lineitem = group.Key.Lineitem,
                        Weightage = group.Key.Weightage,
                        //Result = group.ToList()
                        RowNumber=group.FirstOrDefault().RowNumber
    
                    }).ToList().Where(z => z.Weightage == 1);

    • Marked as answer by Sudip_inn Saturday, February 9, 2019 4:04 PM
    Saturday, February 9, 2019 12:32 PM