我正在尝试从AspNetUsers表中获取名字和姓氏字符串值以获取特定记录(ID).当我使用Linq to Sql时,它将返回整个sql查询字符串.这是我的代码:
var user = User.Identity.GetUserId();
var currentUser = from aspNetUser in db.AspNetUsers
where aspNetUser.Id == user
select aspNetUser.FirstName + " " + aspNetUser.LastName;
我正在尝试使用当前用户的名字和姓氏在视图中填充用户文本框.我已将其作为列添加到AspNetUsers表中.为什么它返回这个丑陋的查询,而不返回某人的名字和姓氏?
它返回这个:
SELECT CASE WHEN ([Extent1].[FirstName] IS NULL) THEN N'' ELSE [Extent1].[FirstName] END + N' ' + CASE WHEN ([Extent1].[LastName] IS NULL) THEN N'' ELSE [Extent1].[LastName] END AS [C1] FROM [dbo].[AspNetUsers] AS [Extent1] WHERE [Extent1].[Id] = @p__linq__0
不太确定如何返回SQL查询,但是您需要从查询中返回一条记录.使用First / FirstOrDefault,例如:
var currentUser = (from aspNetUser in db.AspNetUsers
where aspNetUser.Id == user
select aspNetUser.FirstName + " " + aspNetUser.LastName)
.FirstOrDefault();
FirstOrDefault
将返回从结果返回的第一条记录.如果没有记录,那么您会null
回来.
您也可以使用Single
\ SingleOrDefault
,因为您要查询,ID
并且始终应该返回一条记录.但是,如果有多个符合您条件的记录,它们都可能引发异常.根据您的要求使用它们中的任何一个.
FirstOrDefault
.添加后,我得到了用户的名字和姓氏.谢谢.您可以FirstOrDefault()
根据特定条件获取单个记录:
var currentUser = db.AspNetUsers.FirstOrDefault(x=>x.Id == user);
var Name = currentUser.FirstName + " " + currentUser.LastName;
I am trying to get the First and Last Name string values out of my AspNetUsers table for a particuler record (ID). When I use Linq to Sql, it returns the entire sql query string. here is my code:
var user = User.Identity.GetUserId();
var currentUser = from aspNetUser in db.AspNetUsers
where aspNetUser.Id == user
select aspNetUser.FirstName + " " + aspNetUser.LastName;
I am trying to populate my User Textbox in my View with the Current Users First and Last Name. Which I have added as columns to the AspNetUsers Table. Why does it return this ugly query and not someone's first and last name?
it's returning this:
SELECT CASE WHEN ([Extent1].[FirstName] IS NULL) THEN N'' ELSE [Extent1].[FirstName] END + N' ' + CASE WHEN ([Extent1].[LastName] IS NULL) THEN N'' ELSE [Extent1].[LastName] END AS [C1] FROM [dbo].[AspNetUsers] AS [Extent1] WHERE [Extent1].[Id] = @p__linq__0
Not really sure how you are getting the SQL query back, but you need a single record back from your query. Use First/FirstOrDefault like:
var currentUser = (from aspNetUser in db.AspNetUsers
where aspNetUser.Id == user
select aspNetUser.FirstName + " " + aspNetUser.LastName)
.FirstOrDefault();
FirstOrDefault
would return the first record returned from the result. If there are no records then you will get null
back.
You can also use Single
\ SingleOrDefault
, since you are querying against ID
and there should always be a single record returned. But both of them could throw an exception if there are multiple records matching your criteria. Use any of them according to your requirement.
FirstOrDefault
. Adding that gave me the First and Last Name of the User. Thank you.you can use FirstOrDefault()
to get single record against specific criteria:
var currentUser = db.AspNetUsers.FirstOrDefault(x=>x.Id == user);
var Name = currentUser.FirstName + " " + currentUser.LastName;
本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文