Returning a single value using LINQ C# -
this question has answer here:
- returning single value linq sql 4 answers
i'm trying return value using linq instead i'm returning actual query.
here code:
var email = p in dbcontext.people p.userid == userid select new { p.email }; contact.email = email.tostring(); i want actual email address of user instead i'm getting query string back.
"select [extent1].[userid] [userid], [extent1].[email] [email] [dbo].[person] [extent1] [extent1].[userid] = @p__linq__0"
use firstordefault. returns first element of sequence, or default value if sequence contains no elements.:
var email = (from p in dbcontext.people p.userid == userid select new { p.email }).firstordefault(); also if want email returned can return , there no need use anonymous type purpose.
var email = (from p in dbcontext.people p.userid == userid select p.email).firstordefault();
Comments
Post a Comment