使用 DataContext 获取单个项目
我正在执行以下操作:
public MyItem FetchSingleItem(int id)
{
string query = "SELECT Something FROM Somewhere WHERE MyField = {0}";
IEnumerable<MyItem> collection = this.ExecuteQuery<MyItem>(query, id);
List<MyItem> list = collection.ToList<MyItem>();
return list.Last<MyItem>();
}
这实际上并不是很优雅,我希望有更好的方法可以使用 DataContext 获取单个项目。我从我的存储库中的 DataContext
进行扩展。在你问之前有一个合理的理由,但这不是这个问题的重点;)
那么,有更好的方法吗?
干杯
I'm doing the following:
public MyItem FetchSingleItem(int id)
{
string query = "SELECT Something FROM Somewhere WHERE MyField = {0}";
IEnumerable<MyItem> collection = this.ExecuteQuery<MyItem>(query, id);
List<MyItem> list = collection.ToList<MyItem>();
return list.Last<MyItem>();
}
It's not very elegant really and I was hoping there's something a little better to get a single item out using DataContext
. I'm extending from DataContext
in my repository. There's a valid reason why before you ask, but that's not the point in this question ;)
So, any better ways of doing this?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果是 SQL Server,请将 SQL 更改为:
或者,将这些行更改
为这一行:
If it is SQL Server, change your SQL to:
Or alternatavely, change these lines
into this one:
返回的记录未定义,因为您没有 ORDER BY。所以很难做出准确的翻译。不过,一般来说,颠倒顺序并采用 First():
关系表是无序的。因此,如果您没有精确指定所需的记录,则必须将返回的记录视为随机选择的。
The record returned is undefined, since you have no ORDER BY. So it's hard to do an exact translation. In general, though, reverse the order and take the First():
Relational tables are unordered. So if you don't specify the record you want precisely, you must consider the returned record as randomly selected.