views:

228

answers:

3

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;
    }
+1  A: 

Well, the most obvious problem is that you're creating an instance of an anonymous type. Try this:

public static IQueryable<Guid> NextCaseNoteID (int personID)
{
    var context = new MatrixDataContext();

    IQueryable<Guid> nextNoteID = (from caseNote in context.tblCaseNotes
                                   where caseNote.PersonID == personID
                                   orderby caseNote.InsertDate
                                   select caseNote.CaseNoteID).Skip(1).Take(1);

    return nextNoteID;
}

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 of Take(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...

Jon Skeet
No I want the actual Guid but I was having the same type of issues and I didn't want to bombard my post with my MANY failed attempts at this. I believe that is what Joel Splosky advised, anyway. What would FirstOrDefault() do as far as my attempts and iterating through.... Thanks
Refracted Paladin
Actually, I just realized that using Guid is what I tried at first but it wouldn't compile and `intelisense`(ReSharper??) suggested I change noteID's Type to IQueryable<Guid>....
Refracted Paladin
`FirstOrDefault` will execute the query and return the first result *or* the default value for the type (i.e. `default(Guid)` in this case).
Jon Skeet
yep, I get that now. How do I make it go to the next one and the next one after that?
Refracted Paladin
No it isn't a string. I am really sorry for the Confusion. See my current edit, which I will leave alone now, and you will see where I am at. Sorry!
Refracted Paladin
A: 

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.

shahkalpesh
+1  A: 

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.

EduardoMello