Returning a single value using LINQ C# -


this question has answer here:

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

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -