Linq Join似乎没有完整联接的数据

时间:2019-06-27 02:59:30

标签: c# linq

我修改了此代码(在灰色部分下方),原始代码位于https://www.youtube.com/watch?v=gwD9awr3NNo

有点复杂。

问题在于代码的这一部分(下面完整的代码):

var groupJoin =
                    from owner in owners
                    orderby owner.OwnerID
                    join animal in animals on owner.OwnerID
                    equals animal.OwnerID into ownerGroup
                    select new
                    {
                        Owner = owner.Name,
                        Animals = from owner2 in ownerGroup
                              select owner2
                    };

ownerGroup不应同时包含所有者和动物信息(这是一个联接,联接意味着它应同时包含两者)。

因此,假设这是真的,是什么导致动物被限制为仅来自动物的信息(没有拥有者的信息)? 我推论的方法是: Intellisense显示x(请参阅下面的完整代码)。 只拥有动物类的信息

查看行://Console.WriteLine("* {0}“,x.Name); <-不起作用

namespace LinqTutorialByDerekBanas
{

    class Program
    {

        static void Main(string[] args)
        {

            bool RunMe;

            QueryAnimalData();
        }

        class Animal
        {
            public string AnimalName { get; set; }
            public double Weight { get; set; }
            public double Height { get; set; }
            public int OwnerID { get; set; }

            public Animal(string name = "No Name",
                double weight = 0,
                double height = 0)
            {
                AnimalName = name;
                Weight = weight;
                Height = height;
            }

            public override string ToString()
            {
                return string.Format("{0} weighs {1}lbs and is {2} inches tall",
                    AnimalName, Weight, Height);
            }
        }


        class Owner
        {
                public string Name { get; set; }
                public int OwnerID { get; set; }
        }

            static void QueryAnimalData()
            {
                Animal[] animals = new[]
                {
                    new Animal{AnimalName = "German Shepherd",
                    Height = 25,
                    Weight = 77,
                    OwnerID = 1},
                    new Animal{AnimalName = "Chihuahua",
                    Height = 7,
                    Weight = 4.4,
                    OwnerID = 2},
                    new Animal{AnimalName = "Saint Bernard",
                    Height = 30,
                    Weight = 200,
                    OwnerID = 3},
                    new Animal{AnimalName = "Pug",
                    Height = 12,
                    Weight = 16,
                    OwnerID = 1},
                    new Animal{AnimalName = "Beagle",
                    Height = 15,
                    Weight = 23,
                    OwnerID = 2}
                };

                Owner[] owners = new[]
                {
                    new Owner{Name = "Doug Parks",
                    OwnerID = 1},
                    new Owner{Name = "Sally Smith",
                    OwnerID = 2},
                    new Owner{Name = "Paul Brooks",
                    OwnerID = 3}
                };



                // Create a group inner join
                // Get all animals and put them in a
                // newly created owner group if their
                // IDs match the owners ID 
                var groupJoin =
                    from owner in owners
                    orderby owner.OwnerID
                    join animal in animals on owner.OwnerID
                    equals animal.OwnerID into ownerGroup
                    select new
                    {
                        Owner = owner.Name,
                        Animals = from owner2 in ownerGroup
                                  select owner2
                    };

                int totalAnimals = 0;

                foreach (var ownerGroup in groupJoin)
                {
                    Console.WriteLine(ownerGroup.Owner);
                    foreach (var x in ownerGroup.Animals)
                    {
                        totalAnimals++;
                        Console.WriteLine("* {0}", x.AnimalName);
                        //Console.WriteLine("* {0}", x.Name);  <-- DOES NOT WORK

                    }
                }
            }

     }
}

1 个答案:

答案 0 :(得分:0)

join ... intoGroupJoin的查询语法。组联接是一种LINQ构造,它使一个项目与一组相关项目相关(例如,使用1到许多外键)。

因此在查询中,ownerGroup(我将其命名为animaljoinaj)是{{1}与单个所有者相关的animals的集合}。

OwnerID

对我来说毫无意义-为什么您要重命名Animals = from owner2 in ownerGroup select owner2 owner2?由于Animals是用词不当,实际上是一组ownerGroup,因此,我不确定您打算在这里做什么。

考虑实际实现组联接的扩展方法(这是animal版本,但Enumerable与此类似):

Queryable

这结合了两个序列(public static IEnumerable<TResult> GroupJoin<TOuter,TInner,TKey,TResult> ( this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector, Func<TOuter,IEnumerable<TInner>,TResult> resultSelector); outer),使用两个lambda提取匹配的键,然后为每个inner发送outer和所有匹配的{{ 1}})outer到结果lambda。