如何在foreach循环中使用linq lambda C#MVC剃刀从关系数据库中枚举用户选择的专业名称
本文关键字:选择 用户 枚举 关系数据库 C#MVC 循环 foreach lambda linq 剃刀 | 更新日期: 2023-09-27 18:13:24
各位程序员,您好!
当谈到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;
现在我必须把这个变成林克兰布达。这就是我所做的,它没有任何错误。
控制器内:
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调用我的数据库。
In-ViewModel:
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();
这里有一个LINQ替代方案:
var thisresult = from t in _contentService.Titles
join ut in _contentService.UserTitles on t.titleid equals ut.titleid
where ut.userid == currentUserId
select new SelectListItem
{ Display = t.title, Value = t.titleId };
然而,正如Jon所指出的,如果你只使用一个属性(title(,你不需要创建IEnumerable<SelectListItem>
,你可以只使用IEnumerable<string>
,而不是选择新的SelectListItem。。。您将拥有Select t.title;