使用Entity Framework选择多个列

时间:2023-02-06 09:46:12

Maybe an easy question, but can't find it easily so forgive me =) I try to select multiple columns. The statement I use is:

也许一个简单的问题,但无法轻易找到它原谅我=)我尝试选择多个列。我使用的声明是:

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

Obviously, this doesn't even compile. What is the correct syntax? I also tried method based, and even tough this syntax seems correct, when accessing it throws an 'Unable to cast the type 'Anonymous type' to type 'AIM.PInfo'. LINQ to Entities only supports casting EDM primitive or enumeration types.' exception.

显然,这甚至都没有编译。什么是正确的语法?我也试过基于方法,甚至强硬这个语法似乎是正确的,当访问它时会抛出'无法强制转换类型'匿名类型'来键入'AIM.PInfo'。 LINQ to Entities仅支持转换EDM原语或枚举类型。例外。

Any ideas?

有任何想法吗?

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();

7 个解决方案

#1


73  

Indeed, the compiler doesn't know how to convert this anonymous type (the new { x.ServerName, x.ProcessID, x.Username } part) to a PInfo object.

实际上,编译器不知道如何将此匿名类型(新的{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();

This gives you a list of objects (of anonymous type) you can use afterwards, but you can't return that or pass that to another method.

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

If your PInfo object has the right properties, it can be like this :

如果您的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();

Assuming that PInfo has at least those three properties.

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

Both query allow you to fetch only the wanted columns, but using an existing type (like in the second query) allows you to send this data to other parts of your app.

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

#2


13  

You can select to an anonymous type, for example

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

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

Or you can create a new class that will represent your selection, for example

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

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

then you can for example do the following

那么你可以举例如下

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

#3


4  

Why don't you create a new object right in the .Select:

为什么不在.Select中创建一个新对象:

.Select(x => new PInfo{ 
    ServerName = x.ServerName, 
    ProcessID = x.ProcessID, 
    UserName = x.Username }).ToList();

#4


3  

You either want to select an anonymous type:

您要么选择匿名类型:

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

But you cannot cast that to another type, so I guess you want something like this:

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

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
               };

#5


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

#6


0  

Salam. It's correct way to get data in specified type:

萨拉姆。获取指定类型的数据是正确的方法:

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 
              });

for more information see: The entity cannot be constructed in a LINQ to Entities query

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

#7


-1  

Here is a code sample:

这是一个代码示例:

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();

#1


73  

Indeed, the compiler doesn't know how to convert this anonymous type (the new { x.ServerName, x.ProcessID, x.Username } part) to a PInfo object.

实际上,编译器不知道如何将此匿名类型(新的{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();

This gives you a list of objects (of anonymous type) you can use afterwards, but you can't return that or pass that to another method.

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

If your PInfo object has the right properties, it can be like this :

如果您的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();

Assuming that PInfo has at least those three properties.

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

Both query allow you to fetch only the wanted columns, but using an existing type (like in the second query) allows you to send this data to other parts of your app.

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

#2


13  

You can select to an anonymous type, for example

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

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

Or you can create a new class that will represent your selection, for example

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

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

then you can for example do the following

那么你可以举例如下

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

#3


4  

Why don't you create a new object right in the .Select:

为什么不在.Select中创建一个新对象:

.Select(x => new PInfo{ 
    ServerName = x.ServerName, 
    ProcessID = x.ProcessID, 
    UserName = x.Username }).ToList();

#4


3  

You either want to select an anonymous type:

您要么选择匿名类型:

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

But you cannot cast that to another type, so I guess you want something like this:

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

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
               };

#5


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

#6


0  

Salam. It's correct way to get data in specified type:

萨拉姆。获取指定类型的数据是正确的方法:

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 
              });

for more information see: The entity cannot be constructed in a LINQ to Entities query

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

#7


-1  

Here is a code sample:

这是一个代码示例:

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();