1

While writing a LINQ query i'm unable to find why the output of the query using into keyword is coming like.Please go through the code once and solve my problem.

 class Person_1
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

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


 static void GroupJoinMethod()
        {
            Person_1 magnus = new Person_1 { LastName = "Hedlund",FirstName = "Magnus"  };
            Person_1 terry = new Person_1 {LastName = "Adams", FirstName = "Terry" };
            Person_1 charlotte = new Person_1 { LastName = "Weiss", FirstName = "Charlotte" };
            Person_1 arlene = new Person_1 {LastName = "Huff", FirstName = "Arlene" };
            Person_1 dummy = new Person_1 { LastName = "Williams", FirstName = "Magnus" };

            Pet barley = new Pet { Name = "Barley",Owner="Terry"};//, Owner = terry };
            Pet boots = new Pet { Name = "Boots",Owner="Terry"};//, Owner = terry };
            Pet whiskers = new Pet { Name = "Whiskers",Owner="Charlotte"};//, Owner = charlotte };
            Pet bluemoon = new Pet { Name = "Blue Moon",Owner="Terry"};//, Owner = terry };
            Pet daisy = new Pet { Name = "Daisy",Owner="Magnus"};//, Owner = magnus };

            List<Person_1> peoples = new List<Person_1> { magnus, terry, charlotte, arlene,dummy };
            List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

 Console.WriteLine("\n\nVia Select into\n\n");
            var result2 = from people in peoples
                          join pet in pets
                          on people.LastName equals pet.Owner
                          into gj
                          select new { people.FirstName };

            foreach (var str in result2)
            {
                Console.WriteLine(str.FirstName);
            }
}

Output:

Via Select into

Magnus
Terry
Charlotte
Arlene 
Magnus

Why the output is coming when my join condition is not correct.when i remove into there is no output. What's the impact of into keyword in this.Please help

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2485435
  • 125
  • 9
  • A join with the `into` keyword is called a **GroupJoin**. See: http://msdn.microsoft.com/en-us/library/bb311040.aspx – Loetn Jan 16 '15 at 07:51
  • you don't seem to be using the grouped variable `gj` – Vignesh.N Jan 16 '15 at 07:55
  • You run through people. You try to group them by pets but in the end you grab people.FirstName. The grouping aint being used – dev hedgehog Jan 16 '15 at 08:01
  • See the duplicate. So in short: the output with `into` is not "not correct", i.e. correct, because it is an outer join of sorts. – Gert Arnold Jan 16 '15 at 08:43
  • Thank u so much for the link Soner Sir.......Now i understood why i couldn't able to use pet.Owner while creating anonymous type..... – user2485435 Jan 16 '15 at 08:50

3 Answers3

1
Into

The Into keyword allows creating a temporary variable to store the results of a group, join, or select clause into a new variable.

             var em = from e in emp
                      group e by new{ e.DeptId}
                      into gEmp 
                      where gEmp.Count() > 1
                      select new { gEmp.Key.DeptId, salary = gEmp.Sum(t => t.Salary) };

In the above query, after applying into on grouping, it creates a IGrouping type gEmp variable, which is used to apply the next filter.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

Using the into keyword you "save" the result join into the gj variable and the select is performed on the original set peoples. indeed you retrieve all the first names of the people

Davide Lettieri
  • 326
  • 2
  • 13
0

MSDN Words:Thanks to Soner Sir for the reference http://msdn.microsoft.com/en-us/library/bb311040.aspx A group join produces a hierarchical result sequence, which associates elements in the left source sequence with one or more matching elements in the right side source sequence. A group join has no equivalent in relational terms; it is essentially a sequence of object arrays. If no elements from the right source sequence are found to match an element in the left source, the join clause will produce an empty array for that item. Therefore, the group join is still basically an inner-equijoin except that the result sequence is organized into groups. If you just select the results of a group join, you can access the items, but you cannot identify the key that they match on. Therefore, it is generally more useful to select the results of the group join into a new type that also has the key name, as shown in the previous example.

user2485435
  • 125
  • 9