61

也许是一个简单的问题,但不容易找到所以请原谅我 =) 我尝试选择多个列。我使用的语句是:

var dataset2 = from recordset in entities.processlists 
               where recordset.ProcessName == processname 
               select recordset.ServerName, recordset.ProcessID, recordset.Username;

显然,这甚至无法编译。什么是正确的语法?我也尝试过基于方法,甚至强硬的这种语法似乎是正确的,当访问它时会抛出“无法将类型“匿名类型”转换为类型“AIM.PInfo”。LINQ to Entities 仅支持转换 EDM 基元或枚举类型。例外。

有任何想法吗?

var dataset = entities.processlists
             .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
             .Select(x => new { x.ServerName, x.ProcessID, x.Username })
             .Cast<PInfo>().ToList();
4

7 回答 7

121

实际上,编译器不知道如何将此匿名类型(new { x.ServerName, x.ProcessID, x.Username }部分)转换为 PInfo 对象。

var dataset = entities.processlists
    .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
    .Select(x => new { x.ServerName, x.ProcessID, x.Username }).ToList();

这为您提供了以后可以使用的对象列表(匿名类型),但您不能将其返回或将其传递给另一个方法。

如果您的 PInfo 对象具有正确的属性,它可以是这样的:

var dataset = entities.processlists
    .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
    .Select(x => new PInfo 
                 { 
                      ServerName = x.ServerName, 
                      ProcessID = x.ProcessID, 
                      UserName = x.Username 
                 }).ToList();

假设 PInfo 至少具有这三个属性。

这两个查询都允许您仅获取所需的列,但使用现有类型(如在第二个查询中)允许您将此数据发送到应用程序的其他部分。

于 2013-10-23T08:11:55.480 回答
20

您可以选择匿名类型,例如

var dataset2 = 
    (from recordset in entities.processlists 
    where recordset.ProcessName == processname 
    select new
    {
        serverName = recordset.ServerName,
        processId = recordset.ProcessID, 
        username = recordset.Username
    }).ToList();

或者您可以创建一个新类来代表您的选择,例如

public class MyDataSet
{
    public string ServerName { get; set; }
    public string ProcessId { get; set; }
    public string Username { get; set; }
}

那么您可以例如执行以下操作

 var dataset2 = 
    (from recordset in entities.processlists 
    where recordset.ProcessName == processname 
    select new MyDataSet
    {
        ServerName = recordset.ServerName,
        ProcessId = recordset.ProcessID, 
        Username = recordset.Username
    }).ToList();
于 2013-10-23T08:06:01.490 回答
7

为什么不在以下位置创建一个新对象.Select

.Select(x => new PInfo{ 
    ServerName = x.ServerName, 
    ProcessID = x.ProcessID, 
    UserName = x.Username }).ToList();
于 2013-10-23T08:08:36.420 回答
4

这是获取指定类型数据的正确方法:

var dataset = entities.processlists
         .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
         .Select(x => new { x.ServerName, x.ProcessID, x.Username })
         .ToList() /// To get data from database
         .Select(x => new PInfo()
              { 
                   ServerName = x.ServerName, 
                   ProcessID = x.ProcessID, 
                   Username = x.Username 
              });

有关详细信息,请参阅: 无法在 LINQ to Entities 查询中构造实体

于 2016-01-13T16:13:41.830 回答
3

您要么想选择匿名类型:

var dataset2 = from recordset 
               in entities.processlists 
               where recordset.ProcessName == processname 
               select new 
               {
                recordset.ServerName, 
                recordset.ProcessID, 
                recordset.Username
               };

但是你不能把它转换成另一种类型,所以我想你想要这样的东西:

var dataset2 = from recordset 
               in entities.processlists 
               where recordset.ProcessName == processname 

               // Select new concrete type
               select new PInfo
               {
                ServerName = recordset.ServerName, 
                ProcessID = recordset.ProcessID, 
                Username = recordset.Username
               };
于 2013-10-23T08:06:05.697 回答
1
var test_obj = from d in repository.DbPricing
join d1 in repository.DbOfficeProducts on d.OfficeProductId equals d1.Id
join d2 in repository.DbOfficeProductDetails on d1.ProductDetailsId equals d2.Id
    select new
    {
    PricingId = d.Id,
    LetterColor = d2.LetterColor,
    LetterPaperWeight = d2.LetterPaperWeight
    };


http://www.cybertechquestions.com/select-across-multiple-tables-in-entity-framework-resulting-in-a-generic-iqueryable_222801.html
于 2015-11-10T18:02:20.737 回答
-1

这是一个代码示例:

var dataset = entities.processlists
    .Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
    .Select(x => new PInfo 
                 { 
                      ServerName = x.ServerName, 
                      ProcessID = x.ProcessID, 
                      UserName = x.Username 
                 }) AsEnumerable().
               Select(y => new PInfo
               {
                   ServerName = y.ServerName,
                   ProcessID = y.ProcessID,
                   UserName = y.UserName 
               }).ToList();
于 2016-11-22T10:04:24.820 回答