如何在linq-to-sql中返回一个匿名类型

ron*_*san 6 c# linq linq-to-sql c#-3.0 c#-4.0

我有以下伪代码,但想法是返回一个泛型类

 public static var getSeaOf(string user)
        {
            var periodsSettings = from p in sea
                                  where p.add_by == new Guid(user)
                                  select new { p.id, p.name };

            return var;
        }
Run Code Online (Sandbox Code Playgroud)

我在这里阅读 - 如何从使用LINQ to SQL的c#方法返回匿名类型,本案例的最佳解决方案是为返回类型创建一个类.

但我的问题是,如果我有数百个这样的函数,这是否意味着我需要有数百个类?

我希望有一个更通用的解决方案,谢谢你的帮助!!


我来看看

Silverlight - LinqToEntities - 如何返回匿名类型

但我不能在select new中指定类名,就像文章一样?

public static IEnumerable<retSea> getBskSeasonsOf(string user)
        {
            var periodsSettings = from p in sea
                                  where p.add_by == new Guid(user)
                                  select new retSea { p.id, p.name };

            return periodsSettings;
        }
Run Code Online (Sandbox Code Playgroud)

bra*_*man 2

这是一篇关于使用 VS 2010 的“根据使用情况生成”功能在 .NET 中命名匿名类型的博客文章。

http://diditwith.net/2009/10/24/NamingAnonymousTypesWithGenerateFromUsage.aspx