如何在LINQ中返回查询结果作为特定对象类型 [英] how to return the query result as a specific object type in LINQ
问题描述
当一个linq查询获取整个Entity时,
如何将结果作为对象类型返回?
我的意思不仅仅是一个字段,而是返回该特定记录的所有字段,作为一个实体。
例如sql中的Staff表。返回一个员工对象实体。
如果我写这样的Linq代码:
var db =(from c in context.Staff
其中c.id == sId
选择新的{s.sId,s.staffNo,s.name,s.surname,s。图片});
return db; --->这里
提供错误,因为我想将结果返回为Staff对象。
如何作为表列对象返回?
谢谢
根据您的意见,您需要从方法返回的单个对象。
要返回单项您有以下选项。
-
第一个
:从集合中返回第一个项目,如果没有匹配的项目,则抛出异常。 -
FirstOrDefault
:像第一个
一样工作,除非它会返回null
如果没有匹配的记录。 -
单个
:从列表中只返回一个项目,如果集合包含多个项目或集合为空,它将抛出异常。可能的用法是在查询主键时。只有一个记录应该退回。有超过一个记录的情况应该是特例。 -
SingleOrDefault
:与Single
相同的主体工作,除非该集合为空,否则返回null 。
所有这些方法都可以在查询语法结尾添加,也可以接收谓词。喜欢:
var db =(from c in context.Staff
其中c.id == sId
选择c).FirstOrDefault();
或
var db = context.Staff.FirstOrDefault(c => c.id == sId);
如果您的字段 id
是主键然后使用 SingleOrDefault
。
(旧答案
如果您想从表格返回特定的列,那么您无法将结果投射到您的课程 Staff
,因为这是通过框架生成的。 / strong>
您有两个选项,首先创建另一个类,其属性与结果集相同,并返回其对象。
或者不选择特定列,选择所有列,然后返回员工
类型对象。
var db = from c in context.Staff
其中c.id == sId
select c;
return db;
如果要创建一个其他类,可以说 PartialStaff
然后你可以做到:
var db = from c in context.Staff
其中c.id == sId
选择新的PartialStaff
{
Id = s.sId,
//.......res的属性
};
但是,您的方法返回类型应为 PartialStaff
类型集合。
Sorry I'm new to LINQ and Entity framework.
When a linq query fetches a whole Entity, how can i return the result as an object type? 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. To return a staff object entity.
if i write Linq code like this:
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.
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.FirstOrDefault
: Works same likeFirst
except it would returnnull
if there are no matching records.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. A possible usage is when querying against primary key. Only one record should be returned. Exceptional case should be when there are more than one record.SingleOrDefault
: Works on the same principal asSingle
except it would return null if the collection is empty.
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
.
(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.
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.
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:
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.
这篇关于如何在LINQ中返回查询结果作为特定对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!