我的 DAL 应该返回 DataTables 还是我...无论如何<>?
编辑 1
我很抱歉,但在阅读了 2 篇建议的文章后,我仍然不明白我应该使用什么。 我知道由于各种原因,使用 IQueryable 并不是首选,但这是否也消除了 IEnumerable ? DataTable 真的是我的最佳选择吗?
简而言之,我猜,首选的返回类型是什么?
我有以下简单的 LINQ 查询,我想将其抽象为 DAL。 var 的类型是什么,因此我的方法应该是什么类型?
ConnectDBDataContext context = new ConnectDBDataContext();
var lName = textEdit1.Text;
var searchByPersonLName = from c in context.tblPersons
where c.LastName == lName
orderby c.LastName
select new { c.FirstName,c.LastName,c.PersonID};
dataGridView1.DataSource = searchByPersonLName;
当我将鼠标悬停在 VS 中的它上面时,它会显示 IQueryable
,但是当我放入断点并运行它时,它似乎将自己称为 IEnumerable。 哪一个是正确的,我应该如何声明我的方法?
像这样-->
public static DataTable SearchPerson(string SearhParam)
{
ConnectDBDataContext context = new ConnectDBDataContext();
var persons = (from person in context.tblPersons
orderby person.LastName
select new { person.PersonID, person.LastName, person.FirstName, person.SSN });
var filteredPersonsList = persons.Where(p => p.LastName == SearhParam).ToList();
if (filteredPersonsList.Count == 0)
filteredPersonsList = persons.Where(p => p.LastName.StartsWith(SearhParam)).ToList();
var dataTable = filteredPersonsList.CopyLinqToDataTable();
return dataTable;
}
如果我使用 IQueryable
什么是
或者我如何知道它以及我会返回什么?
谢谢!
作为参考,CopyToDataTable() 如下。
public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source)
{
return new ObjectShredder<T>().Shred(source, null, null);
}
public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source,
DataTable table, LoadOption? options)
{
return new ObjectShredder<T>().Shred(source, table, options);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,IQueryable 实现了 IEnumerable,因此这就是您可能会看到两者的原因。 请参阅此处了解更多详细信息
一般来说,我会推荐您的 DAL尽可能返回您的实际对象。
我会阅读此博客< /a> 了解如何做以及如何不做您所建议的事情的指南。 简短的回答,不要返回 IQueryable。
编辑:
例子:
First off, IQueryable implements IEnumerable, so that is why you are potentially seeing both. See here for more details
Generally, I would recommend your DAL return your actually objects whenever possible.
I would read this blog for guidelines on how to, and how not to do what you are suggesting. Short answer, don't return IQueryable.
EDIT:
Example:
他的意思是将您的数据映射到您希望 DAL 返回的对象。
在回答你的第一个问题时,“var”实际上只是变量的缩写,类型是赋值中定义的类型。
在此示例中,类型是字符串。
而在此示例中,类型是 StringReader 的类型。
至于你的第二个问题“是什么”。 T 是泛型类型。
有关 dal 将返回实际对象的示例:
What he means is to map your data to the object you are wanting the DAL to return.
In answer to your first question "var" is really just short for variable, and the type is what ever type is defined in the assignment.
In this example the type is that of a string.
While in this example the type is that of a StringReader.
As for your second question of "what is ". T is a generic type.
For an example of where your dal would be returning an actual object: