none
LINQ Challenge Query RRS feed

  • Question

  •  

    List liF = new List(); liF.Add(new Foo { Name = "John", ResultType = "Positive", Specimen = "48474" });
     liF.Add(new Foo { Name = "Jack", ResultType = "Positive", Specimen = "57575" });
     liF.Add(new Foo { Name = "Mary", ResultType = "Positive", Specimen = "59056" });
     liF.Add(new Foo { Name = "John", ResultType = "Positive", Specimen = "48475" }); 
    liF.Add(new Foo { Name = "John", ResultType = "Positive", Specimen = "56757" });
    


    Inline solution is great!!!!!

    Desired Output when queried with "Positive"

    John 48474,48475,56757

    Jack 57575

    Mary 59056


    SV

    Tuesday, August 30, 2011 7:34 PM

Answers

  • Hi vai2000;

    The following Linq to Object will do what you are looking for using method syntax.

    var results = liF.Where( rt => rt.ResultType == "Positive")
             .GroupBy (n => n.Name)
             .Select ( g => new { 
                  Key = g.Key, 
                  Specimen = g.Aggregate(String.Empty, (s, next) => 
                       s + ", " + next.Specimen).Substring(1) 
                 });
                 
    foreach (var element in results)
    {
      Console.WriteLine( element.Key + "\t\t" + element.Specimen );
    }
    

    The above query will result in the following output from the foreach loop.

    John     48474, 48475, 56757
    Jack     57575
    Mary     59056

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by vai2000 Wednesday, August 31, 2011 1:01 PM
    Wednesday, August 31, 2011 4:14 AM

All replies

  •  

     

    var output = from l in liF
                             where l.ResultType == "Positive"
                             group l by l.Name
                             into g
                             select new {Result = g.Key, s = (from a in liF where a.Name == g.Key select a.Specimen) };

     

    // To see what is in the result

    var sb = new StringBuilder();
                foreach(var o in output)
                {              
                    sb.Append(o.Result + " ");
                    foreach (var specimen in o.s)
                        sb.Append(specimen + ",");

                    sb.Append(";");
                }

                var s = sb.ToString().Split(';');

    Tuesday, August 30, 2011 9:58 PM
  • Hi vai2000;

    The following Linq to Object will do what you are looking for using method syntax.

    var results = liF.Where( rt => rt.ResultType == "Positive")
             .GroupBy (n => n.Name)
             .Select ( g => new { 
                  Key = g.Key, 
                  Specimen = g.Aggregate(String.Empty, (s, next) => 
                       s + ", " + next.Specimen).Substring(1) 
                 });
                 
    foreach (var element in results)
    {
      Console.WriteLine( element.Key + "\t\t" + element.Specimen );
    }
    

    The above query will result in the following output from the foreach loop.

    John     48474, 48475, 56757
    Jack     57575
    Mary     59056

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by vai2000 Wednesday, August 31, 2011 1:01 PM
    Wednesday, August 31, 2011 4:14 AM
  • Awesome!
    SV
    Wednesday, August 31, 2011 1:02 PM
  •  

    Glad I was able to help. Have a nice day.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 31, 2011 1:23 PM