【发布时间】:2015-01-01 08:17:50
【问题描述】:
我正在使用 EF6,我的模型名称之一是 tblAttachLabel。我已经定制了名为 AttachLabel 的模型。我需要我的自定义模型的 IEnumerable 填充 tblAttachLabel 模型。很容易退货
IEnumerable<tblAttachLabel>
来自我的函数,但我需要返回
IEnumerable<AttachLabel>
所以我做这个代码:
public static IEnumerable<AttachLabel> GetAttachLabel()
{
Entities db = new Entities();
var x = from al in db.tblAttachLabels select al;
List<AttachLabel> temp = new List<AttachLabel>();
IEnumerable<AttachLabel> _attachLabel;
foreach (var item in x)
{
AttachLabel a = new AttachLabel()
{
ID = item.ID,
Text = item.Text
};
temp.Add(a);
}
_attachLabel = temp;
return _attachLabel;
}
但我知道当我使用 List 作为 temp 时,查询会执行,但我不想要这个。那么我怎样才能返回一个 IEnumerable 呢?
【问题讨论】:
-
but I know when I use a List for temp the query will execute这是什么意思?你可以直接返回temp应该没问题(因为List<T>实现了IEnumerable<T>) -
查询将在
foreach中执行。为什么不希望执行此查询?
标签: c# asp.net linq ienumerable