Note that there are some explanatory texts on larger screens.

plurals
  1. POlinq query returns too many results
    text
    copied!<p>EDIT: This question is now solved</p> <p>I am currently trying to do a simple selection of multiple columns from various tables using LINQ, which I am very new to. 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.</p> <p>So if the output was supposed to say something like (this is just an example)</p> <pre><code>Name Task Name Status Derp Do the Dishes ACTIVE John Example Task COMPLETE Jesus Walk on Water IMPOSSIBLE </code></pre> <p>it would instead say</p> <pre><code>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 </code></pre> <p>I assume there is something wrong with my LINQ syntax.</p> <p>Here is the relevant part of my controller:</p> <pre><code>var TaskInstanceList = taskInstanceService.SelectAll(); var Person = personService.SelectAll(); var Task = taskService.SelectAll(); var Status = statusService.SelectAll(); var NewModel = new TaskLogModelContainer(); NewModel.Tasks = new List&lt;TaskLogModel&gt;(); 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); </code></pre> <p>and here is my view</p> <pre><code>@model MyProj.Models.TaskLogModelContainer @{ ViewBag.Title = "index"; } &lt;h2&gt;@ViewBag.Title&lt;/h2&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; Task Name &lt;/th&gt; &lt;th&gt; Status &lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model.Tasks) { &lt;tr&gt; &lt;td&gt;@item.FirstName @item.LastName&lt;/td&gt; &lt;td&gt;@item.TaskName&lt;/td&gt; &lt;td&gt;@item.Status&lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload