gpt4 book ai didi

c# - linq 查询返回太多结果

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:36 26 4
gpt4 key购买 nike

编辑:这个问题现在已经解决了

我目前正在尝试使用 LINQ 从不同的表中简单地选择多个列,我对此还很陌生。基本上,我使用内部联接合并来自多个表的结果,以便将其中一些信息提供给我的模型,然后我将这些信息传递给 View 。查询只返回第一个结果(这是一个正确的结果)并且返回它的次数比它出现的次数多,同时从不显示任何其他结果。也就是说,它会多次重复第一个结果,而不会显示任何其他结果。

所以如果输出应该是这样的(这只是一个例子)

Name     Task Name   Status
Derp Do the Dishes ACTIVE
John Example Task COMPLETE
Jesus Walk on Water IMPOSSIBLE

它会说

Name     Task Name   Status
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE
Derp Do the Dishes ACTIVE

我假设我的 LINQ 语法有问题。

这是我的 Controller 的相关部分:

var TaskInstanceList = taskInstanceService.SelectAll();
var Person = personService.SelectAll();
var Task = taskService.SelectAll();
var Status = statusService.SelectAll();

var NewModel = new TaskLogModelContainer();
NewModel.Tasks = new List<TaskLogModel>();

var results = from ti in TaskInstanceList
join p in Person on ti.personID equals p.personID
join t in Task on ti.taskID equals t.taskID
join s in Status on ti.task_statusID equals s.statusID
select new { Person = p, Task = t, Status = s, Instance = ti };

foreach (var result in results)
{
var obj = new TaskLogModel();
obj.ID = result.Instance.person_taskID;
obj.FirstName = result.Person.FirstName;
obj.LastName = result.Person.LastName;
obj.Description = result.Task.Description;
obj.TaskName = result.Task.Name;
obj.Value = result.Task.Value;
obj.Status = result.Status.Status;
obj.Notes = result.Status.Notes;

NewModel.Tasks.Add(obj);
}

return View(NewModel);

这是我的看法

@model MyProj.Models.TaskLogModelContainer

@{
ViewBag.Title = "index";
}

<h2>@ViewBag.Title</h2>

<table>
<tr>
<th>
Name
</th>
<th>
Task Name
</th>
<th>
Status
</th>
</tr>

@foreach (var item in Model.Tasks)
{
<tr>
<td>@item.FirstName @item.LastName</td>
<td>@item.TaskName</td>
<td>@item.Status</td>
</tr>
}
</table>

最佳答案

如果您的 Linq 语句返回太多结果,或者输出中的数据量与查询不符,请说明。

如果您的 Linq 语句返回太多结果,您可以将查询减少为单个连接。所以开始:

  var results = from ti in TaskInstanceList
join p in Person on ti.personID equals p.personID

然后

  var results = from ti in TaskInstanceList
join t in Task on ti.taskID equals t.taskID

备注:此文需加评论-本人名誉不允许写

关于c# - linq 查询返回太多结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17608568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com