使用 LINQ to SQL 实现下一个/上一个
我有下面的代码,试图允许用户通过单击 WinForm 上的“下一步”或“上一步”来“单步执行”数据库中的案例注释。它只会获取第一个案例注释。 我做错了什么?
对这篇文章进行了多次编辑,我很抱歉,但在遵循乔恩·斯基特的建议时,我能够“修复”最初的错误,但它仍然不起作用。
我是否需要重组查询以考虑当前注释?如果是这样,我该怎么做?
public static Guid NextCaseNoteID (int personID)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID
orderby caseNote.InsertDate
select caseNote.CaseNoteID ).Skip(1).FirstOrDefault();
return nextNoteID;
}
这就是我最终得到的结果,感谢每个发布并遵循我的不良思路的人...
它似乎工作得很好,尽管我现在试图证明我是否仍然需要 Skip(1)... 谢谢!!
供将来参考
public static Guid NextCaseNoteID (int personID, DateTime? insertDate)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID && caseNote.InsertDate > insertDate
orderby caseNote.InsertDate
select caseNote.CaseNoteID ).Skip(1).FirstOrDefault();
return nextNoteID;
}
public static Guid PreviousCaseNoteID(int personID, DateTime? insertDate)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID && caseNote.InsertDate < insertDate
orderby caseNote.InsertDate
select caseNote.CaseNoteID).Skip(1).FirstOrDefault();
return nextNoteID;
}
I have the code below in an attempt to allow the user to "Step Through" the Case Notes in the DB by clicking Next or Previous on the WinForm. It will grab the First Case Note only.
What am I doing wrong?
There has been numerous edits to this post, I apologize, but in following Jon Skeet's advice I was able to "fix" what was originally wrong but it still doesn't work.
Do I need to restructure my query to take into account the current note? If so, how do I do that?
public static Guid NextCaseNoteID (int personID)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID
orderby caseNote.InsertDate
select caseNote.CaseNoteID ).Skip(1).FirstOrDefault();
return nextNoteID;
}
This is what I ended up with, thanks to everyone who posted and followed my ill thought train of thought...
It seems to work well though I am now trying to prove if I need the Skip(1) still....
Thanks!!
for future reference
public static Guid NextCaseNoteID (int personID, DateTime? insertDate)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID && caseNote.InsertDate > insertDate
orderby caseNote.InsertDate
select caseNote.CaseNoteID ).Skip(1).FirstOrDefault();
return nextNoteID;
}
public static Guid PreviousCaseNoteID(int personID, DateTime? insertDate)
{
var context = new MatrixDataContext();
Guid nextNoteID = (from caseNote in context.tblCaseNotes
where caseNote.PersonID == personID && caseNote.InsertDate < insertDate
orderby caseNote.InsertDate
select caseNote.CaseNoteID).Skip(1).FirstOrDefault();
return nextNoteID;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,最明显的问题是您正在创建匿名类型的实例。试试这个:
我完全不确定这是否真的是您想要的,但它可能至少可以编译...
您确定不想返回实际的 GUID
IQueryable
的?您可能想调用
FirstOrDefault()
而不是Take(1)
...编辑:好的,所以它确实返回一个 GUID.. . 你说它不起作用,但没有说它不起作用。如果您想获取下一个案例注释,您很可能应该传递案例注释 ID 而不是人员 ID,但这并不是很清楚......
Well, the most obvious problem is that you're creating an instance of an anonymous type. Try this:
I'm not at all sure that it's really what you're after, but it's likely to at least compile...
Are you sure you don't want to return the actual GUID instead of an
IQueryable<Guid>
?You might want to call
FirstOrDefault()
instead ofTake(1)
...EDIT: Okay, so it does return a GUID... you say it's not working, but not how it's not working. If you want to fetch the next case note, you should quite possibly pass in the case note ID rather than the person ID, but it's not terribly clear...
如果想要下一个/上一个方法,您不应该告知现在的当前位置吗?
然后跳过 currentPosition + 1 进行下一个和 currentPosition - 1。
我通常使用 Single() 而不是 FirstOrDefault()。但我认为这没有什么区别。
If want a next/previous method, shouldn't you be informing what is the current position now?
And then skipping currentPosition + 1 for next and currentPosition - 1.
I usually uses Single() instead of FirstOrDefault(). But I think it will make no difference.
我可能听起来像个白痴。但是,为什么不能使用 datareader 来执行此操作(尤其是当您使用 linq to sql 时,它又调用 SQL Server 来执行此操作)?
如果我完全误解了这个问题,请告诉我。
I might sound like an idiot. But, why can't you use a datareader to do this thing (esp. when you are using linq to sql, which in turn calls SQL Server to do this)?
Let me know, if I have totally misunderstood the problem.