0

I am not able to display a table like this

classId      class name    frequency
1            basic one      2
2            basic two      1

I have a model like this to use to display frequency , classsId and class name. the classsId is a primary key in the class table and foreign key in the studentDetails table.className is in the class table

public int ClasssId { get; set; }       
public int ClassCount { get; set; }

i create an action method to fetch data from the database and group

public ActionResult ClasssStatistics()
{
     IQueryable<ClassStatistics> data = from st in db.StudentDetailss
     group st by st.ClasssId into classsGroup
     select new ClassStatistics()
     {
         ClasssId = classsGroup.Key,
         ClassCount = classsGroup.Count()
     };
     return View(data.ToList());
 }

and this is my view and the result i generate

<thead>
     <tr>
        <th>Class Id</th>
              <th>Frequency</th>
     </tr>
</thead>

<tbody>
   @foreach (var item in Model)
   {
       <tr class="odd gradeX">
           <td>@Html.DisplayFor(modelitem => item.ClasssId)</td>
           <td>@item.ClassCount</td>
       </tr>
   }
</tbody>

result

classs Id      frequency
1                2
2                1
ekad
  • 14,436
  • 26
  • 44
  • 46
  • 2
    Please stop claiming that arbitrary bits of text are Javascript snippets. They're not. (You can quote code without making it a Javascript snippet. Here, there's nothing that should be viewed as a runnable snippet.) – Jon Skeet Jun 02 '15 at 10:22
  • 1
    What's the actual question here? Why not just add the class name with a join or something like that? – fk2 Jun 02 '15 at 10:25

1 Answers1

0

It's a little bit naughty but if you're confident that the classes will be the same throughout then you can just get the first from the group - there will always be one otherwise the group wouldn't exist.

public ActionResult ClasssStatistics()
{
    IQueryable<ClassStatistics> data = from st in db.StudentDetailss
                                       group st by st.ClasssId into classsGroup
                                select new ClassStatistics()
                                {
                                    ClassId = classsGroup.Key,
                                    ClassName = classsGroup.First().ClassName,
                                    ClassCount = classsGroup.Count()
                                };
    return View(data.ToList());

}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
Stu
  • 2,426
  • 2
  • 26
  • 42
  • thanks.based on your responce, i added className to the model. and i added this line to my action method. "ClassName = classsGroup.First().ClassName," the ClassName at the right handside is being underlined as error.dont mind my naughty question.am a beginner – akanbi awwal Jun 02 '15 at 10:38
  • and possibly if am to use join statement.how do i acheive that – akanbi awwal Jun 02 '15 at 10:43
  • i modified the line of code and no more underline "ClassName = classsGroup.First().Classs.ClasssName," but returns this error after running it. "The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead." – akanbi awwal Jun 02 '15 at 10:53
  • i have gotten it.your solution really helps.i used FirstOrDefault instead of First(). and it worked.thanks again.this is my first time of asking question in a forum.pls can u recommend good books on LINQ to teach me better.how will i know if somene has posted a comment under my comment – akanbi awwal Jun 02 '15 at 11:09
  • 1
    Glad you got it to work. I can't recommend a LINQ book I'm afraid, sorry. The MS Press step-by-step books have usually served me well in the past for learning languages. I think you get notified of the response by e-mail if you don't see it. If you're finished please mark the question as answered so it doesn't appear on unanswered question lists. – Stu Jun 02 '15 at 12:12