2

I have multiple list of class.

public class MainClass
{
 public List<A> a{ get; set; }
 public List<B> b{ get; set; }
 public List<C> c{ get; set; }
}

And main class contains following property

 class A
    {
      public string Name{ get; set; }
      public string MainAddress{get;set;}
    }

    class B
    {
      public int ID {get;set;}
      public string Address{ get; set; }
    }

    class C
    {
     public int ID {get;set;}
      public string Name{ get; set; }
    }

Now how do i fetch data from List b & c and pass it to A This is what i tried

  List<A> query = (from b in MainClass.b
               join c in Mainclass.c
                on b.ID equals c.ID
                select new {b.Address, c.Name});

How do i display Name from C as MainAddress in A

Richa
  • 3,261
  • 2
  • 27
  • 51

3 Answers3

2

Correct me if I'm wrong but I think that what you want is a List of A having MainAddresses comming from the c list. If so:

 var query = (from b in MainClass.b
               join c in Mainclass.c
                on b.ID equals c.ID
                select new A { MainAddress = c.Name });

Edit: I went ahead and made a full working example so that you can check your mistakes...

  • A, B, and C classes must have the same visibility as a, b, and c properties.
  • You are accessing b and c statically but they are instance variables.

Of course the transformation from Enumerable to List can be made in one step by calling the ToList selector directly on the Enumerable instance.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var mainClass = new MainClass();

            mainClass.b = new List<B>();
            mainClass.c = new List<C>();
            mainClass.b.Add(new B { ID = 1, Address = "B1" });
            mainClass.c.Add(new C { ID = 1, Name = "C1" });

            //using LINQ
            var query = (from b in mainClass.b
                         join c in mainClass.c
                          on b.ID equals c.ID
                         select new A { MainAddress = c.Name });

            List<A> fromLINQ = query.ToList();

            //using anonymous functions aka lambda expressions
            IEnumerable<A> enumerableFromAnonymous = mainClass.b.Join(mainClass.c, x => x.ID, x => x.ID, (x, y) => { return new A() { MainAddress = y.Name }; });
            List<A> listFromAnonymous = enumerableFromAnonymous.ToList();
        }
    }

    public class MainClass
    {
        public List<A> a { get; set; }
        public List<B> b { get; set; }
        public List<C> c { get; set; }
    }

    public class A
    {
        public string Name { get; set; }
        public string MainAddress { get; set; }
    }

    public class B
    {
        public int ID { get; set; }
        public string Address { get; set; }
    }

    public class C
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
Sam Aleksov
  • 1,201
  • 6
  • 12
2

Heres a complete Snippet of the demo code: http://share.linqpad.net/aq8f6c.linq

To get the address from B displayed as the Mainadress in A do the following:

var result = from a in As
             join b in (from b in Bs
                        join c in Cs
                        on b.ID equals c.ID
                        select new {b.Address, c.Name})
             on a.Name equals b.Name
             select new A {
                 Name = a.Name,
                 MainAddress = b.Address
             };

result then is of type Ienumerable<A>.

Output:

enter image description here

Marco
  • 22,856
  • 9
  • 75
  • 124
0

I thought Serv's example was the correct answer but I did not like the query syntax as I find this more difficult to read and maintain due to Visual Studio's inability to support Lambda expressions in the immediate window - as of VS2013.

I prefer method syntax using an intermediate variable. This saves screen space and I think it is easier to understand:

   var query = from a in As
                join c in (from b in Bs
                           join c in Cs
                           on b.ID equals c.ID
                           select new {b.Address, c.Name})
                on a.Name equals c.Name
                select new A {
                    Name = a.Name,
                    MainAddress = c.Address
                };
    query.Dump();

    //method sytnax
    var nameAddress = Bs.Join(Cs, b => b.ID, c => c.ID, (b, c) => new { Name = c.Name, Address = b.Address });
    var query2 = As.Join(nameAddress, a => a.Name, na => na.Name, (a, na) => new { Name = a.Name, MainAddress = na.Address});
    query2.Dump();
Jaycee
  • 3,098
  • 22
  • 31