Linq记录到字符串数组

本文关键字:数组 字符串 记录 Linq | 更新日期: 2023-09-27 18:17:54

这是我的LINQ:

var context = new CYGNETEntities();
var users = (from u in context.mUsers
             where u.userName == EmailID && u.password == Password
             select new 
             { 
                 u.userName,  
                 u.firstName,  
                 u.lastName  
             })
    .AsEnumerable()
    .Select(x => string.Format("{0}, {1}, {2}", x.userName, x.firstName, x.lastName))
    .ToArray();

这里,实际值如下:

   u.userName = "admin"
   u.firstName = "fname"
   u.lastName = "lname"
因此,linq返回如下:
users[0] = admin, fname, lname

但是我想要linq记录在字符串数组中,如下所示:

   users[0] = admin
   users[1] = fname
   users[2] = lname

有什么建议吗?

Linq记录到字符串数组

我还没有测试过这个,但是如何:

var users = (from u in context.mUsers
              where u.userName == EmailID && u.password == Password
              from s in new string [] { u.username, u.FirstName, u.LastName }
              select s).ToArray();