AmiR HN
AmiR HN

Reputation: 55

IQueryable does not contain a definition for SingleOrDefault

I created a simple Login Form in C# with Entity Framework. I keep getting this error in the if statement where it calls query.SingleOrDefault

Error CS1061 'IQueryable' does not contain a definition for 'SingleOrDefault' and no accessible extension method 'SingleOrDefault' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)

I'm relatively new to this, how do I fix this error?

using DevExpress.XtraEditors;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Data.Entity;

namespace myHome
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void linkSite_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.pishdad.org");
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                XtraMessageBox.Show("نام کاربری وارد نشده است", "پیام", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUser.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(txtPass.Text))
            {
                XtraMessageBox.Show("رمز کاربری وارد نشده است", "پیام", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPass.Focus();
                return;
            }


            try

            {
                IQueryable query = null;
                using (myHomeEntities db = new myHomeEntities())
                {
                    query = from u in db.TB_User
                        where u.UserName == txtUser.Text && u.PassWord == txtPass.Text
                        select u;

                }


                if (query.SingleOrDefault() != null) // This line is the issue.
                {
                    XtraMessageBox.Show("شما دسترسی پیدا کردید! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    XtraMessageBox.Show("ورود نا  معتبر! ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Upvotes: 3

Views: 4390

Answers (2)

asathkum
asathkum

Reputation: 305

The accepted answer didn't explain the change very well, so I think that merits this post.

Amir, you tried to pass the non-generic IQueryable as an argument to SingleOrDefault() which requires an IEnumerable<T>.

Afshin resolved your error by changing your IQueryable to a IQueryable<T>, and then converted it to an IEnumerable<T> via ToList().

You could have also used Queryable.Cast<T>() to get the desired result:

query.Cast<T>().ToList().SingleOrDefault(); // T being your desired Type

Upvotes: 2

Afshin Rashidi
Afshin Rashidi

Reputation: 353

using this:

try
{
   IQueryable<TB_User> query = null;
   ...


  if (query.ToList().SingleOrDefault() != null)
  {
     ...
  }

}

Upvotes: 2

Related Questions