[英]how to return the query result as a specific object type in LINQ
Sorry I'm new to LINQ and Entity framework. 抱歉,我是LINQ和Entity框架的新手。
When a linq query fetches a whole Entity, how can i return the result as an object type? 当linq查询获取整个实体时,如何将结果作为对象类型返回? I mean not just a field but also to return all the fields of that specific record, as one entity.
我的意思是不仅是一个字段,而且还作为一个实体返回该特定记录的所有字段。
For example a Staff table in sql. 例如sql中的Staff表。 To return a staff object entity.
返回人员对象实体。
if i write Linq code like this: 如果我这样写Linq代码:
var db= (from c in context.Staff
where c.id == sId
select new { s.sId,s.staffNo,s.name,s.surname,s.image});
return db; ---> here
Gives an error because i want to return result as Staff object. 给出错误,因为我想将结果作为Staff对象返回。
How can it return as a table column object? 如何将其作为表列对象返回?
Thank you 谢谢
As per your comments, you need a single object returned from your method. 根据您的注释,您需要从方法中返回一个对象。
To return a single Item you have following options. 要返回单个项目,您可以使用以下选项。
First
: Returns first item from the collection, if there are no matching items then an exception would be thrown. First
:从集合中返回第一个项目,如果没有匹配的项目,则将引发异常。 FirstOrDefault
: Works same like First
except it would return null
if there are no matching records. FirstOrDefault
:与First
相似,但如果没有匹配的记录将返回null
。 Single
: Returns only one item from the list, It will throw exception if the collection contains more than one item or the collection is empty. Single
:仅返回列表中的一项,如果该集合包含多个项或该集合为空,则将引发异常。 A possible usage is when querying against primary key. SingleOrDefault
: Works on the same principal as Single
except it would return null if the collection is empty. SingleOrDefault
:与Single
具有相同的主体,不同之处在于如果集合为空,它将返回null。 All these methods can be added at the end of your query syntax or could receive a predicate. 所有这些方法都可以在查询语法的末尾添加,也可以接收谓词。 Like:
喜欢:
var db= (from c in context.Staff
where c.id == sId
select c).FirstOrDefault();
Or 要么
var db = context.Staff.FirstOrDefault(c=> c.id == sId);
If your field id
is a primary key then use SingleOrDefault
. 如果您的字段
id
是主键,则使用SingleOrDefault
。
(Old Answer (旧答案
If you are trying to return particular columns from a table, then you can't project your result to your class Staff
since that is generated through the framework. 如果您尝试从表中返回特定的列,则无法将结果投影到类
Staff
因为它是通过框架生成的。
You have two options. 您有两个选择。 First create another class with properties same as your result set and return its object.
首先创建另一个具有与结果集相同属性的类,然后返回其对象。
Or instead of selecting specific columns, select all columns and then return Staff
type object. 或者,而不是选择特定的列,而是选择所有列,然后返回
Staff
类型的对象。
var db= from c in context.Staff
where c.id == sId
select c;
return db;
If you want to create an other class lets say PartialStaff
then you can do: 如果要创建另一个类,请说
PartialStaff
则可以执行以下操作:
var db= from c in context.Staff
where c.id == sId
select new PartialStaff
{
Id = s.sId,
//.......rest of the properties
};
But then your method return type should be PartialStaff
type collection. 但是,然后您的方法返回类型应该是
PartialStaff
类型集合。
When a linq query fetches a whole Entity, how can i return the result as an object type?
当linq查询获取整个实体时,如何将结果作为对象类型返回? I mean not just a field but also to return all the fields of that specific column, as one entity.
我的意思是不仅是一个字段,而且还作为一个实体返回该特定列的所有字段。
For example a Staff table in sql.
例如sql中的Staff表。 To return a staff object entity.
返回人员对象实体。
You could try this one: 您可以尝试以下方法:
var db= (from c in context.Staff
where c.id == sId
select c);
or 要么
db = context.Staff.Where(s=>s.id == sId);
The result of the above query is a sequence of Staff objects, IEnumerable<Staff>
. 上面查询的结果是一个Staff对象的序列 ,即
IEnumerable<Staff>
。 Specifically, this sequence will contain all the Staff
objects in context.Staff
, whose id
is equal to sId
. 具体来说,此序列将包含
context.Staff
所有Staff
对象,其id
等于sId
。
Gives an error because i want to return result as Staff object.
给出错误,因为我想将结果作为Staff对象返回。
Now If you are sure that there would be at most one item in context.Staff
with the given id
, you could try this: 现在,如果您确定
context.Staff
中最多有一项。具有给定id
,您可以尝试以下操作:
var db = (from c in context.Staff
where c.id == sId
select c).SingleOrDefault();
or 要么
var db = context.Staff.SingleOrDefault(s=>s.id == sId);
This query will return the single Staff
object with the given id
and if there isn't any Staff
object in context.Staff
with this id, will return null
. 这个查询将返回单个
Staff
对象与给定id
,如果没有任何Staff
在物体context.Staff
与此ID,将返回null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.