我在Linq to SQL中有问题要返回查询吗?在方法中 [英] I have a problem in Linq to SQL to return a query? in method

查看:63
本文介绍了我在Linq to SQL中有问题要返回查询吗?在方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请阅读我在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);
                    }
                }
            }


此代码产生以下输出:

马格努斯岛Hedlund:
黛西
亚当斯,特里:
大麦
靴子
夏洛特·魏斯:
Whiskers

解决方案

您可以使用动态关键字作为返回类型.但是,这通过反射使用了后期绑定,因此您在编译时无法获得智能感知的好处.为什么不创建一个强类型的对象并将结果投影到该类型的实例上呢?

  var 查询=
                people.GroupJoin(宠物,
                                 人= > 人,
                                 pet = >  pet.Owner,
                                 (人员,petCollection)= > 
                                      PetOwners
                                     {
                                         OwnerName = person.Name,
                                         宠物= petCollection.Select(宠物= >  pet.Name)
                                     });


如果要在多种情况下使用同一查询,请尝试避免创建匿名类型.这是一个很好的链接,也可能会对您有所帮助.
http://msmvps. com/blogs/jon_skeet/archive/2009/01/09/horrible-grotty-hack-returning-an-anonymous-type-in​​stance.aspx [ http://stackoverflow.com/questions/534690/linq-to-sql-return-匿名类型 [ ^ ]

所以您需要定义一个这样的类:

 公共  class  PersonPet
{
    公共 字符串 OwnerName { get ; 设置; }
    公共列表< string>宠物{获取; 设置; }
} 



返回类型为 IQueryable<PersonPet>

您的代码应如下所示:

 IQueryable< PersonPet>查询=
    people.GroupJoin(宠物,
                     人= > 人,
                     pet = >  pet.Owner,
                     (人员,petCollection)= > 
                          PersonPet()
                         {
                             OwnerName = person.Name,
                             宠物= petCollection.Select(宠物= >  pet.Name)
                         });



我无法对其进行测试,因此可以根据需要进行一些操作.


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

解决方案

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?

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[^]


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 :

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 :

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.


这篇关于我在Linq to SQL中有问题要返回查询吗?在方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆