[英]How to get first character from firstName and lastName from Database and assign to object?
I am new to this. 我是新来的。 I am getting data from database and storing each tuple in the following way to form a list:
我从数据库中获取数据并以以下方式存储每个元组以形成列表:
var dataFromDB = (
from to in _context.Time
join user in _context.Users
on to.UserId equals user.Id
select new Events
{
userId = user.Id,
title = user.FirstName[0] + " " + user.LastName[0],
...
}).ToList();
I want title
to have the initials from FirstName
and LastName
where FirstName
and LastName
are coming from the Users
table. 我希望
title
具有FirstName
和LastName
的缩写,其中FirstName
和LastName
来自Users
表。 Example, if the FirstName
is John
and LastName
is Doe
, then I want title
to have JD
. 例如,如果
FirstName
是John
而LastName
是Doe
,那么我希望title
具有JD
。
Events
class is in the following way: Events
类采用以下方式:
public class CalendarEvents
{
public string userId { get; set; }
public string title { get; set; }
...
}
Edit 1.0 编辑1.0
On running the code, I am getting the following exception: 运行代码时,出现以下异常:
An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code.
mscorlib.dll中发生类型'System.ArgumentNullException'的异常,但未在用户代码中处理。
Additional information: Value cannot be null.
附加信息:值不能为null。
Edit 2.0 编辑2.0
FirstName
and LastName
is in the following way: FirstName
和LastName
通过以下方式:
[Required(ErrorMessage = "Please enter first name")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter last name")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
If you want the first initial and second initial I would suggest: 如果您想要第一个首字母和第二个首字母,我建议:
title = user.FirstName.FirstOrDefault().ToString()
+ user.LastName.FirstOrDefault().ToString(),
Since, user.FirstName.FirstOrDefault()
returns the first character from the FirstName
. 因为,
user.FirstName.FirstOrDefault()
从FirstName
返回第一个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.