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

LINQ to SQL - How to select specific columns and return strongly typed list(LINQ to SQL - 如何选择特定列并返回强类型列表)
本文介绍了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 - 如何选择特定列并返回强类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)