1

I need LINQ code to be able to detect if an entry is input but the datum already exists in the table.

public Boolean CheckAssessment(String assessmentName)
{
    {
        SQL_TA_SCOREBOARDEntities1 d = new SQL_TA_SCOREBOARDEntities1();
        Assessment A_List = new Assessment();
        {

            var qry2 = from b in contxt.View_Assessment
                       where b.AssessmentName == assessmentName
                       select b;


            if (qry2 = assessmentName.ToString())
            {
                return true;                         
            }
            else
            {
                return false;
            }
        }
    }
}

Where assessmentName is the value of the textbox in the a separate C# class.

Boolean i;
i = TAClass.CheckAssessment(txtAssessmentName.ToString());

if (i == true)
{
    Label2.Text = "Assessment name already exists.";
}
1
  • 1
    Your code can be improved a lot as you can see in my answer. But, just to make it clear for you, your code would not work because of two reasons: 1) the linq can return more than one result (you should use the FirstOrDefault extension) and 2) you need to select b.AssessmentName in linq code, otherwise it will return an object for you (and not a string) Commented Feb 14, 2014 at 2:50

2 Answers 2

2

The correct way is to use Any() extension method. Any() will generate optimized query and will return true as soon as the query found the record.

http://msdn.microsoft.com/en-us/library/bb534338.aspx

So, in your CheckAssessment method, you can do like this:

        return (from b in assessments
                where b.AssessmentName == assessmentName
                select b).Any();
Sign up to request clarification or add additional context in comments.

Comments

1

Use the Count() extension to check if there are any matching records:

public bool CheckAssessment(string assessmentName)
{
    return (from b in contxt.View_Assessment
        where b.AssessmentName == assessmentName
        select b).Count() > 0;
}

UPDATE: just for your knowledge, your code could work this way:

public Boolean CheckAssessment(String assessmentName)
{
  {
    SQL_TA_SCOREBOARDEntities1 d = new SQL_TA_SCOREBOARDEntities1();
    Assessment A_List = new Assessment();
    {

        var qry2 = (from b in contxt.View_Assessment
                   where b.AssessmentName == assessmentName
                   select b.AssessmentName).FirstOrDefault();


        if (qry2 = assessmentName.ToString())
        {
            return true;                         
        }
        else
        {
            return false;
        }
    }
  }
}

Just one more comment, always that you have something like:

if (a) { return true; } else {return false;}

you can replace by

 return a;

2 Comments

Code-only answers don't give the full picture. Helps to have an explanation to go with the code.
Thanks for the helpful comment. I will remember that. And thank you for the edition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.