none
Join Tolist() result and First() result RRS feed

  • Question

  • Hi,

    I have 2 linq result sets. One variable (var a) has result retuned as ToList().

    the other var has result returned as FirstOrDefault().

    Both have separate columns. I want to merge both , such that columns in 2nd get appended to all the rows of 1st result.

    Can u suggest how this can be accomplished.   

    Tuesday, June 7, 2011 6:42 PM

Answers

  • Hi  Van ,

     

    I guess below code may give you some pointer. I am sure there would be better way to join both result set though

          DataClasses1DataContext context = new DataClasses1DataContext();
          var result = from r in context.Persons select r ;
          StudentGrade result1 = ((from r in context.StudentGrades select r).FirstOrDefault());     
    
          foreach (var r in result)
          {
            Console.WriteLine(r.PersonID + result1.Person.LastName );
          }
         
          Console.ReadKey(true);
    

     

    Thanks with Regards

    Dhananjay Kumar

    http://debugmode.net/


    Thanks Dhananjay Kumar
    MVP Connected System Developer http://dhananjaykumar.net/
    Contact: dhananjay.kumar@live.com Twitter: http://twitter.com/debugmode_
    Wednesday, June 8, 2011 6:27 AM
  • Hello van,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace App
    {
      public class Foo
      {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public override string ToString()
        {
          return "Fied1 = " + this.Field1 + " - Field2 = " + this.Field2;
        }
      }
    
      public class Bar
      {
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public override string ToString()
        {
          return "Fied3 = " + this.Field3 + " - Field4 = " + this.Field4;
        }
      }
    
      class Program
      {
        static void Main(string[] args)
        {
          List<Foo> foos = new List<Foo>{
            new Foo { Field1 = "f1", Field2 = "Foo1"},
            new Foo { Field1 = "f2", Field2 = "Foo2"},
            new Foo { Field1 = "f3", Field2 = "Foo3"},
            new Foo { Field1 = "f4", Field2 = "Foo4"}
          };
    
          List<Bar> bars = new List<Bar>{
            new Bar { Field3 = "b1", Field4 = "Bar1"},
            new Bar { Field3 = "b2", Field4 = "Bar2"},
            new Bar { Field3 = "b3", Field4 = "Bar3"},
            new Bar { Field3 = "b4", Field4 = "Bar4"}
          };
    
          Console.WriteLine("## foos ##");
          List<Foo> fooquery = (from f in foos
                     select f).ToList();
          foreach (Foo f in fooquery) Console.WriteLine(f);
    
          Console.WriteLine("## bar ##");
          Bar barquery = (from b in bars
                  where b.Field3 == "b3"
                  select b).FirstOrDefault();
          if (barquery != null)
            Console.WriteLine(barquery);
    
          Console.WriteLine("## append ##");
          if (barquery != null)
          {
            var query = (from f in fooquery
                   from b in new List<Bar> { barquery }
                   select
                   new
                   {
                     Field1 = f.Field1,
                     Field2 = f.Field2,
                     Field3 = b.Field3,
                     Field4 = b.Field4
                   }).ToList();
    
            foreach (var row in query) Console.WriteLine(row);
          }
    
          Console.ReadKey();
        }
      }
    }
    
    

    Kind regards,


    aelassas.free.fr
    Wednesday, June 8, 2011 5:11 PM

All replies

  • Maybe you can try Projection with LINQ
    Tuesday, June 7, 2011 8:55 PM
  • I think you'd better use foreach to access it
    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, June 8, 2011 3:49 AM
  • Hi  Van ,

     

    I guess below code may give you some pointer. I am sure there would be better way to join both result set though

          DataClasses1DataContext context = new DataClasses1DataContext();
          var result = from r in context.Persons select r ;
          StudentGrade result1 = ((from r in context.StudentGrades select r).FirstOrDefault());     
    
          foreach (var r in result)
          {
            Console.WriteLine(r.PersonID + result1.Person.LastName );
          }
         
          Console.ReadKey(true);
    

     

    Thanks with Regards

    Dhananjay Kumar

    http://debugmode.net/


    Thanks Dhananjay Kumar
    MVP Connected System Developer http://dhananjaykumar.net/
    Contact: dhananjay.kumar@live.com Twitter: http://twitter.com/debugmode_
    Wednesday, June 8, 2011 6:27 AM
  • Hello van,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace App
    {
      public class Foo
      {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public override string ToString()
        {
          return "Fied1 = " + this.Field1 + " - Field2 = " + this.Field2;
        }
      }
    
      public class Bar
      {
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public override string ToString()
        {
          return "Fied3 = " + this.Field3 + " - Field4 = " + this.Field4;
        }
      }
    
      class Program
      {
        static void Main(string[] args)
        {
          List<Foo> foos = new List<Foo>{
            new Foo { Field1 = "f1", Field2 = "Foo1"},
            new Foo { Field1 = "f2", Field2 = "Foo2"},
            new Foo { Field1 = "f3", Field2 = "Foo3"},
            new Foo { Field1 = "f4", Field2 = "Foo4"}
          };
    
          List<Bar> bars = new List<Bar>{
            new Bar { Field3 = "b1", Field4 = "Bar1"},
            new Bar { Field3 = "b2", Field4 = "Bar2"},
            new Bar { Field3 = "b3", Field4 = "Bar3"},
            new Bar { Field3 = "b4", Field4 = "Bar4"}
          };
    
          Console.WriteLine("## foos ##");
          List<Foo> fooquery = (from f in foos
                     select f).ToList();
          foreach (Foo f in fooquery) Console.WriteLine(f);
    
          Console.WriteLine("## bar ##");
          Bar barquery = (from b in bars
                  where b.Field3 == "b3"
                  select b).FirstOrDefault();
          if (barquery != null)
            Console.WriteLine(barquery);
    
          Console.WriteLine("## append ##");
          if (barquery != null)
          {
            var query = (from f in fooquery
                   from b in new List<Bar> { barquery }
                   select
                   new
                   {
                     Field1 = f.Field1,
                     Field2 = f.Field2,
                     Field3 = b.Field3,
                     Field4 = b.Field4
                   }).ToList();
    
            foreach (var row in query) Console.WriteLine(row);
          }
    
          Console.ReadKey();
        }
      }
    }
    
    

    Kind regards,


    aelassas.free.fr
    Wednesday, June 8, 2011 5:11 PM