2017-08-22 23 views
0

我的查询是获取其出生日期为当前日期的雇员的姓名。 在控制我的LINQ查询:如何声明列表类型变量并将其传递到MVC模型中

  EmployeeModel objUser = new EmployeeModel(); 
     var users = dbContext.EmployeeProfiles.FirstOrDefault(u => u.DOB == DateTime.Now.ToString("dd-MMM")).UserName; 
     objUser.Users = users.ToList(); 
     return View(objUser); 

如何在模型中声明用户

请注意:我的看法是强类型的视图(EmployeeModel的)

+1

'FirstOrDefault()'返回一个对象(或'null'),不是一个集合 - 你想要一个'。凡()'和一个'。选择()' –

+0

好感谢斯蒂芬。现在如何在模型中声明它? –

+0

你想要一个单一的名称,这是你的查询返回'(字符串用户)或名称集合('IEnumerable 用户') –

回答

-1

在视图的顶部 - >

@model EmployeeModel 
{ 
    var Users=Model.Users; 
} 
+0

但是如何在EmployeeModel中声明用户? –

+0

因此在EmployeeModel中没有名为Users的字段? – N1gthm4r3

+0

是的。我不知道如何在模型中为此目的声明一个列表类型。 –

0

假设你var usersEmployeeProfile类型的,因为你要查询dbContext.EmployeeProfiles.....

public class EmployeeModel 
    { 
    public List<EmployeeProfile> Users { get; set; } 

    //...some other properties here.... 
    } 

这个答案与我所能做到的一样直截了当,但实际上我会推荐使用ICollection<EmployeeProfile>代替List<EmployeeProfile>

相关问题