LINQ to SQL - 如何选择特定列并返回强类型列表

时间:2023-02-18
本文介绍了LINQ to SQL - 如何选择特定列并返回强类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 LINQ to SQL 从表中选择一些特定的列,并将结果作为强类型对象列表返回.

例如:

var 结果 =(来自 DataContext.Persons 中的 a其中 a. 年龄 >18选择新人{Name = a.Name,年龄 = a.Age}).ToList();

任何帮助将不胜感激.

它构建得很好,但是当我运行它时,出现错误.不允许在查询中显式构造实体类型 MyEntity.

解决方案

基本上,您的做法是正确的.但是,您应该使用 DataContext 的实例进行查询(DataContext 是查询中的实例或类型名称并不明显):

var 结果 = (来自 new DataContext().Persons其中 a. 年龄 >18选择新人 { Name = a.Name, Age = a.Age }).ToList();

显然,Person 类是您的 LINQ to SQL 生成的实体类.如果您只需要某些列,则应该创建自己的类:

class PersonInformation {公共字符串名称 {get;set;}公共整数年龄{get;set;}}var result = (from a in new DataContext().Persons其中 a. 年龄 >18选择新的 PersonInformation { Name = a.Name, Age = a.Age }).ToList();

您可以在此处自由地将 varList 交换而不会影响任何事情(因为这是编译器所做的).

否则,如果您在本地处理查询,我建议考虑使用匿名类型:

var 结果 = (来自 new DataContext().Persons其中 a. 年龄 >18select new { a.Name, a.Age }).ToList();

请注意,在所有这些情况下result静态类型(它的类型在编译时已知).后一种类型是编译器生成的匿名类的 List,类似于我上面写的 PersonInformation 类.从 C# 3.0 开始,语言中没有动态类型.

更新:

如果你真的想返回一个List(这可能是也可能不是最好的做法),你可以这样做:

var result = from a in new DataContext().Persons其中 a. 年龄 >18选择新的 { a.Name, a.Age };列表<人>list = result.AsEnumerable().Select(o => new Person {姓名 = o.Name,年龄 = o.Age}).ToList();

您也可以合并上述语句,但为了清楚起见,我将它们分开了.

I'm trying to use LINQ to SQL to select a few specific columns from a table and return the result as a strongly typed list of objects.

For Example:

var result = (from a in DataContext.Persons
                              where a.Age > 18
                              select new Person
                              {
                                  Name = a.Name,
                                  Age = a.Age
                              }
                              ).ToList();

Any help would be greatly appreciated.

It builds fine, but when I run it, I get the error. Explicit construction of entity type MyEntity in query is not allowed.

解决方案

Basically you are doing it the right way. However, you should use an instance of the DataContext for querying (it's not obvious that DataContext is an instance or the type name from your query):

var result = (from a in new DataContext().Persons
              where a.Age > 18
              select new Person { Name = a.Name, Age = a.Age }).ToList();

Apparently, the Person class is your LINQ to SQL generated entity class. You should create your own class if you only want some of the columns:

class PersonInformation {
   public string Name {get;set;}
   public int Age {get;set;}
}

var result = (from a in new DataContext().Persons
              where a.Age > 18
              select new PersonInformation { Name = a.Name, Age = a.Age }).ToList();

You can freely swap var with List<PersonInformation> here without affecting anything (as this is what the compiler does).

Otherwise, if you are working locally with the query, I suggest considering an anonymous type:

var result = (from a in new DataContext().Persons
              where a.Age > 18
              select new { a.Name, a.Age }).ToList();

Note that in all of these cases, the result is statically typed (it's type is known at compile time). The latter type is a List of a compiler generated anonymous class similar to the PersonInformation class I wrote above. As of C# 3.0, there's no dynamic typing in the language.

UPDATE:

If you really want to return a List<Person> (which might or might not be the best thing to do), you can do this:

var result = from a in new DataContext().Persons
             where a.Age > 18
             select new { a.Name, a.Age };

List<Person> list = result.AsEnumerable()
                          .Select(o => new Person {
                                           Name = o.Name, 
                                           Age = o.Age
                          }).ToList();

You can merge the above statements too, but I separated them for clarity.

这篇关于LINQ to SQL - 如何选择特定列并返回强类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:LINQPad,使用多个数据上下文 下一篇:LINQ 返回列表中与另一个列表中的任何名称(字符

相关文章

最新文章