C# 使用实体框架选择多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19536064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select multiple columns using Entity Framework
提问by Ronald
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();
采纳答案by Réda Mattar
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.
事实上,编译器不知道如何将这个匿名类型(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();
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.
这两个查询都允许您仅获取所需的列,但使用现有类型(如第二个查询中)允许您将此数据发送到应用程序的其他部分。
回答by Andre Lombaard
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();
回答by CodeCaster
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
};
回答by tmp001
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();
回答by Ilker Eker
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
回答by Sayed Abolfazl Fatemi
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 查询中构造实体
回答by Ankit
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();