Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have you already got a database context/repository set up to communicate with the database in your MVC project? Below I'm assuming you've got a database context named dbContext.</p> <p><b>Note: If you're not using Razor, replace the "@" with "&lt;%" and close with "%>" </b></p> <p>You can either put the item in the ViewBag, or in the Model (I prefer Model) like below. I'm not sure what you want to do with these counts, so i've put them in a list. You'll need a class to put your impression info in, i've added it below;</p> <p><b>Model Code: </b></p> <pre><code> public class MyModel { public class ImpressionInfo //just used to store your results sub class of the model { public ImpressionInfo(id, impression, diaryImpressionCount) { Id = id; Impression = impression; DiaryImpressionCount = diaryImpressionCount } public int Id { get; set; } public int Impression { get; set; } //is this an int? you didn't say public int DiaryImpressionCount { get; set; } } public MyModel() { var impressionInfo = new List&lt;ImpressionInfo&gt;() foreach (var di in dbContext.DiaryImpressions) { ImpressionInfos.Add(new ImpressionInfo( di.Id, di.Impression, dbContext.DiaryPosts .Count(dp =&gt; dp.ImpressionsId == di.ID)); } } public List&lt;ImpressionInfo&gt; ImpressionInfos { get; set; } </code></pre> <p>then in the view</p> <p><b>View Code: </b></p> <pre><code> @model MyModel @if(Model.ImpressionInfos.Count &gt; 0) { &lt;table&gt; &lt;tr&gt; &lt;td&gt;Impression&lt;/td&gt; &lt;td&gt;Count&lt;/td&gt; &lt;/tr&gt; foreach (var i in Model.ImpressionInfos) { &lt;tr&gt; &lt;td&gt;@i.Impression&lt;/td&gt; &lt;td&gt;@i.DiaryImpressionCount&lt;/td&gt; &lt;/tr&gt; } } else { &lt;p&gt;No Impression infos&lt;/p&gt; } </code></pre> <p>Hope this helps.</p>
 

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