1

the following code returns all information of table if credentials matches. But I want to write another query inside else condition block where it will return me only specific columns of admins table, for example Email, Name, and Role. How can I do that?

Admin adminLoggedin  = db.Admins.SingleOrDefault(x => x.Email==model.Email && x.Password == model.Password);
if (adminLoggedin == null)
{
    return BadRequest();
}
else
{
    // ***Query2  select Email , Name, RoleId from Admins Where Email = adminLoggedin.Email 
    // how to write this query with Entity framework and ado.net model

    FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
                        return Ok(adminLoggedin);
}

1 Answer 1

1

Using anonymous type and query syntanx, you can write something like this:

var query = from admin in db.Admins
where admin.Email == adminLoggedIn.Email
select new { Email=admin.Email, Name=admin.Name, RoleId = admin.RoleId };

If you need to return this query results from this function, create a simple class with properties Name, Email etc and then use it with select clause in above query e.g. select new CustomClass {Email, admin.Email,}

Sign up to request clarification or add additional context in comments.

Comments

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.