简体   繁体   English

如何从数据库的firstName和lastName获取第一个字符并分配给对象?

[英]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具有FirstNameLastName的缩写,其中FirstNameLastName来自Users表。 Example, if the FirstName is John and LastName is Doe , then I want title to have JD . 例如,如果FirstNameJohnLastNameDoe ,那么我希望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: FirstNameLastName通过以下方式:

[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.

相关问题 如何从FirstName和LastName等数据库中获取SimpleMembershipProvider扩展属性? - How do I get SimpleMembershipProvider extended properties back from database like FirstName and LastName? 从文本中获取姓氏/名字的正则表达式 - Regular Expression to get LastName / FirstName from a text 使用linq从lastname +“,” + firstname的组合中搜索名字 - search firstname from combination of lastname+“, ”+firstname using linq 我们如何使用 linq 查询从 mvc 中的客户表中获取两列的列表,例如名字和姓氏 - How can we get list of two column say firstname and lastname from customer table in mvc using linq query 从Sharepoint中的当前用户获取名字和姓氏 - Get first and lastname from current user in Sharepoint 使用ASP.NET从Facebook获取名字,姓氏和电子邮件地址 - Get firstname, lastname and email address from Facebook using ASP.NET 如何从具有4个单词的名称中分割名字和姓氏? 在C#中并在Mysql中查询使用它 - How to split firstname and lastname from a name which has 4 words.? in C# and query using it in Mysql 当输入全名且实体在 c# 时,如何从 sql 数据库中搜索 FirstName + Lastname? - How to search for FirstName + Lastname from sql data base when input is full name with entities in c#? 在C#中将名字和姓氏与全名字符串分开 - Separate firstname and lastname from fullname string in C# Linq按名字姓氏分组并从子数组中选择 - Linq to group by firstname lastname and select from a sub array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM