【发布时间】:2010-10-24 14:39:35
【问题描述】:
编辑 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<T>,但是当我放入断点并运行它时,它似乎自称 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<T> <T> 是什么,或者我怎么知道,我会返回什么?
谢谢!
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);
}
【问题讨论】:
标签: c# linq linq-to-sql n-tier-architecture