简体   繁体   English

linq查询返回太多结果

[英]linq query returns too many results

EDIT: This question is now solved 编辑:这个问题现在已经解决

I am currently trying to do a simple selection of multiple columns from various tables using LINQ, which I am very new to. 我目前正在尝试使用LINQ从各种表中简单地选择多个列,这对我来说还是很新的。 Basically I am combining results from multiple tables using inner joins in order to give some of that information to my model, which I then pas to a view. 基本上,我使用内部联接组合来自多个表的结果,以便将某些信息提供给我的模型,然后将其传递给视图。 The query only ever returns the first result (which is a correct result) and returns it more times than it appears, while never showing any of the other results. 该查询只返回第一个结果(这是正确的结果),并且返回的次数比显示的次数多,而从不显示任何其他结果。 That is, it repeats the first result multiple times while never displaying any other results. 也就是说,它多次重复第一个结果,而从不显示任何其他结果。

So if the output was supposed to say something like (this is just an example) 因此,如果输出应该像这样(这只是一个例子)

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

it would instead say 它会说

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

I assume there is something wrong with my LINQ syntax. 我认为我的LINQ语法有问题。

Here is the relevant part of my 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);

and here is my view 这是我的看法

@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>

Please clarify, if your Linq statement returns too much results or if the amount of data in the output does not correspond to the query. 请说明您的Linq语句返回的结果是否过多,或者输出中的数据量是否与查询不符。

If your Linq statement returns too much results you may reduce your query to a single join. 如果您的Linq语句返回太多结果,则可以将查询减少为单个联接。 So start with: 因此开始:

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

then 然后

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

Remark: This text should by a comment - my reputation does not allow to write one 备注:此文字应带有注释-我的名声不允许写

SOLVED. 解决了。

One of my SelectAll statements was bugged. 我的SelectAll陈述式有一个错误。 Which is just plain weird, because I have tested them all thorouhly and was using it an hour ago with no problems. 这简直太奇怪了,因为我已经对它们进行了全面的测试,并且在一小时前就使用了它,没有任何问题。 Maybe I accidentally ctrl-z'd something or bumped a key. 也许我不小心按了ctrl-z或撞了键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM