Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please read this code that i found in msdn:

            class Person
            {
                public string Name { get; set; }
            }

            class Pet
            {
                public string Name { get; set; }
                public Person Owner { get; set; }
            }

            public static void GroupJoinEx1()
            {
                Person magnus = new Person { Name = "Hedlund, Magnus" };
                Person terry = new Person { Name = "Adams, Terry" };
                Person charlotte = new Person { Name = "Weiss, Charlotte" };

                Pet barley = new Pet { Name = "Barley", Owner = terry };
                Pet boots = new Pet { Name = "Boots", Owner = terry };
                Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
                Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

                List<person> people = new List<person> { magnus, terry, charlotte };
                List<pet> pets = new List<pet> { barley, boots, whiskers, daisy };

                // Create a list where each element is an anonymous 
                // type that contains a person's name and 
                // a collection of names of the pets they own.
//////////////////////////////////////////////////////////////////////////////////
I want to create a method for this query in below and Instead of this code i use a method!
//////////////////////////////////////////////////////////////////////////////////
                var query =
                    people.GroupJoin(pets,
                                     person => person,
                                     pet => pet.Owner,
                                     (person, petCollection) =>
                                         new
                                         {
                                             OwnerName = person.Name,
                                             Pets = petCollection.Select(pet => pet.Name)
                                         }); 
////////////////////////////////////////////////////////////////////////
               foreach (var obj in query)
                {
                    // Output the owner's name.
                    Console.WriteLine("{0}:", obj.OwnerName);
                    // Output each of the owner's pet's names.
                    foreach (string pet in obj.Pets)
                    {
                        Console.WriteLine("  {0}", pet);
                    }
                }
            }

This code produces the following output:

Hedlund, Magnus:
Daisy
Adams, Terry:
Barley
Boots
Weiss, Charlotte:
Whiskers
Posted
Updated 29-Jan-12 18:54pm
v5
Comments
[no name] 29-Jan-12 21:45pm    
Format code snippets
[no name] 29-Jan-12 21:46pm    
OK, I've read the code.

Now, do you have an actual question?
Mohamed Ahmed Abdullah 30-Jan-12 0:29am    
Where is the problem????
best or not 30-Jan-12 0:49am    
I dont know what kind of return value that i have here to return in my method?

You could use the dynamic key word as your return type. However that uses latebounding through reflection and you don't get the benefit of intellisense at compile time. Why don't you rather create a strongly typed object and project the result to an instance of that type?

C#
var query =
                people.GroupJoin(pets,
                                 person => person,
                                 pet => pet.Owner,
                                 (person, petCollection) =>
                                     new PetOwners
                                     {
                                         OwnerName = person.Name,
                                         Pets = petCollection.Select(pet => pet.Name)
                                     });

Try to avoid creating an anonymous type if you going to use the same query in multiple scenarios. Here's a good link that may also help you.
http://msmvps.com/blogs/jon_skeet/archive/2009/01/09/horrible-grotty-hack-returning-an-anonymous-type-instance.aspx[^]
 
Share this answer
 
Comments
Tech Code Freak 12-Feb-12 1:02am    
5up!
I think this is your answer :
http://stackoverflow.com/questions/534690/linq-to-sql-return-anonymous-type[^]

So you need to define a class like this :

C#
public class PersonPet
{
    public string OwnerName { get; set; }
    public List<string> Pets  { get; set; }
}


And the return type will be IQueryable<PersonPet>

Your code should look like this :
C#
IQueryable<PersonPet> query =
    people.GroupJoin(pets,
                     person => person,
                     pet => pet.Owner,
                     (person, petCollection) =>
                         new PersonPet()
                         {
                             OwnerName = person.Name,
                             Pets = petCollection.Select(pet => pet.Name)
                         });


I can't test it so do some manipulation if it needs.
 
Share this answer
 
v2
Comments
Tech Code Freak 12-Feb-12 1:02am    
5up!
Amir Mahfoozi 12-Feb-12 2:15am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900