Click here to Skip to main content
15,914,350 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, i m doing a c# project with ADO .NET data entity model, i have a table Subjects with columns (idSubject(int), nameSubject(nvarchar), subjectNoted(bit/boolean). I'm getting from the table the first subject that have `subjectNoted==false` then i change the value to `==true` after clickin a button with this code:
C#
private void button1_Click(object sender, EventArgs e)
            {
                using (DbEntities db = new DbEntities())
                {
                Subjects firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false);
                    if (firstSubject != null)
                    {
                        firstSubject.subjectNoted = true;
                        db.SaveChanges();
                        MessageBox.Show("Subject Noted", "OK");                    
                    }
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
        //code to show the next subject that have subjectNoted==false
            }

I have another button, this one must ignore the first subject and show the second one with `subjectNoted==false`. I created a method ShowSubject, but i can't find how to select the second subject that have `subjectNoted==false` and show it with the methode??
C#
private void ShowSubject(Subjects subject)
            {
                using (DbEntities db = new DbEntities())
                {
                   Subjects  firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false);
                    if (firstSubject != null)
                    {
                        textBox1.Text = firstSubject.nameSubject;
                    }
                }
            }

Someone can help me please i'm stuck here!
Posted
Updated 12-May-13 11:10am
v4

Have you tried using Skip() and Take() methods ?

Skip(n) will jump the n number of records and Take(n) will return n many records.

for example you could try
C#
var subjects = db
    .Subjects
    .Where(s=> s.subjectNoted == false)
    .OrderBy(s=> s.idSubject)
    .Skip(1)
    .Take(1)
    .FirstOrDefault();


One thing is you do need to add an order by to your query before you can do a skip / take.
 
Share this answer
 
private void ShowSubject(Subjects subject)
{
using (DbEntities db = new DbEntities())
{
Subjects firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false).Skip(1).Take(1) .FirstOrDefault();
if (firstSubject != null)
{
textBox1.Text = firstSubject.nameSubject;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900