c# - 如何在 foreach 循环中使用 linq lambda C# MVC razor 枚举从关系数据库中选择的专业职称的用户?

标签 c# mysql asp.net-mvc razor lambda

各位程序员,大家好!

对于 MVC C#,我有点新手,但这是我的问题。我有三个关系型 MySQL 表;用户、头衔和用户头衔。

用户看起来像这样:

+---------------+----------+
| currentUserId | UserName |
+---------------+----------+
|             1 | Dave     |
+---------------+----------+

标题如下所示:

+---------+-------+
| TitleId | Title |
+---------+-------+
|       1 | BS    |
|       2 | MD    |
|       3 | PHD   |
|       4 | BA    |
+---------+-------+

...UserTitles 看起来像这样:

+--------+---------+
| UserId | TitleId |
+--------+---------+
|      1 |       1 |
|      1 |       2 |
|      1 |       3 |
|      1 |       4 |
+--------+---------+

现在,在我看来,我想根据数据库中分配给 Dave 的专业名称显示“Dave,BS,MD,PHD,BA”。

现在我可以轻松地在 MySQL 查询中执行此操作,并且它会起作用:

select Title from titles
INNER JOIN UserTitles on UserTitles.TitleId = titles.TitleId
where UserTitles.UserId = currentUserId;

现在我必须将其转换为 Linq.Lambda。这是我做的,没有显示任何错误。

在 Controller 中:

ViewData.Add("FirstName", Users.Name);

var thisresult =
_contentService.Titles.Join(_contentService.UserTitles,
x => x.Id, y => y.TitleId, (x, y) => new {x, y})
.Where(@t => @t.y.UserId == currentUserId)
.Select(@t => new SelectListItem {Value = @t.x.TitleString}).ToList();

更新 (感谢大家的支持)

IEnumerable<String> thisresult = _contentService.Titles
.Join(_contentService.UserTitles.Where(x => x.UserId == currentUserId),
t => t.Id,
ut => ut.TitleId,
(t, ut) => t.TitleString);

var model = new ManageUserViewModel()
{
 Titlelist = thisresult.ToList()
};

return View(model);

其中 _contentService 调用我的数据库。

在 View 模型中:

public IEnumerable<SelectListItem> Titlelist { get; set; }

更新

public IEnumerable<String> Titlelist { get; set; }

查看中:

 @ViewBag.FirstName,
 @foreach (var x in Model.Titlelist)
            {@x.Value}

更新

@foreach (var title in Model.Titlelist)
            {
                @Html.Raw(title)
            }

输出:

“/”应用程序中的服务器错误。 不支持指定的方法。

有什么想法吗?

最佳答案

var thisresult =
_contentService.Titles.Join(_contentService.UserTitles, 
x => x.Id, y => y.TitleId, (x, y) => new {x, y})
.Where(@t => @t.y.UserId == currentUserId)
.Select(@t => new {Title = @t.x.TitleString}).ToList();

将 ToString() 更改为 ToList();

编辑:

var thisresult =
_contentService.Titles.Join(_contentService.UserTitles, 
x => x.Id, y => y.TitleId, (x, y) => new {x, y})
.Where(@t => @t.y.UserId == currentUserId)
.Select(@t => new SelectListItem{
  Selected = //write your stuff here ,
  Text = //write your stuff here //,
  Value = //write your stuff here  }).ToList();

编辑2

var thisresult =  _contentService.Titles.Join(_contentService.UserTitles, 
x => x.TitleId, y => y.TitleId, (x, y) => new {x, y})
.Where(@t => @t.y.UserId == currentUserId)
.Select(@t => new SelectListItem{
  Selected = //write your stuff here ,
  Text = //write your stuff here //,
  Value = //write your stuff here  }).ToList();

关于c# - 如何在 foreach 循环中使用 linq lambda C# MVC razor 枚举从关系数据库中选择的专业职称的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653821/

相关文章:

c# - 使用 HttpClient C# 对许多不同网站的快速 Web 请求

php - Laravel - 使用 bundle 从数据库中检索数据

php - 在表悬停时从 WordPress 数据库获取数据

MySQL 通过 UDF 生成 key 环导致 SQLException

c# - 检查 HtmlHelperMethod 中是否存在(部分) View

c# - HTML 敏捷提取 PHP 标签

c# - 单元测试与集成测试

C#:包含 2 个和/或 3 个单词的名称的正则表达式

c# - 在 ASP.NET 中并行执行 .NET HttpWebRequests 的建议

html - 有没有办法在 C# MVC 中呈现两个局部 View ,使它们并排出现在一个 View 中?