Solved

Search Filter using lamba or linq - group by & take top record for each group.

Posted on 2016-10-27
3
78 Views
Last Modified: 2016-10-28
Hello,

I am having some difficulties with getting the correct statement in lambda or linq.  


SQL Query
select count(me.schools_id) as numOfStudent, schools_name, members_exams_examdate
from members_exams as me
join schools as s on s.schools_id = me.schools_id
where members_exams_examdate = '8/19/16'
group by s.schools_id, members_exams_examdate, schools_name



This returns
+----------------------+---------------------------+-----------------------------------------+
|numOfStudent |  School Name       |  members exams examdate |
| 1                         | School A                |  8/19/2016                                |
| 4                         | School B                |  8/19/2016                                |
| 7                         | School C                |  8/19/2016                                |
| 21                       | School D                |  8/19/2016                               |
+-----------------------+---------------------------+----------------------------------------+


Controller:   Try1
	public ActionResult Index(string searchBy, string search)

			{
				DateTime convertedSearch;
				DateTime.TryParse(search, out convertedSearch);

					if (searchBy == "members_exams_examdate")
					{

						return View(from x in db.members_exams
									where x.members_exams_examdate == (convertedSearch)
									group x by x.schools_id
										into groups
										select groups.OrderBy(p => p.school.schools_name).First());

					
					}

					return View();
			}

Open in new window

Return Error: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.

Try2:   using  FirstorDefault
select groups.OrderBy(p => p.school.schools_name).FirstorDefault());

Open in new window


Return Error: Error      1      'System.Linq.IOrderedEnumerable<Data.members_exams>' does not contain a definition for 'FirstorDefault' and no extension method 'FirstorDefault' accepting a first argument of type 'System.Linq.IOrderedEnumerable<Data.members_exams>' could be found (are you missing a using directive or an assembly reference?)      


Try3:
	return View(from x in db.members_exams
									where x.members_exams_examdate == (convertedSearch)
									group x by x.schools_id into y
									select new
									{
										schools_id = y.Key
									});

Open in new window


Error: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousTypef`1[System.Nullable`1[System.Int16]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Data.members_exams]'.

In the view I would like to group the records and display only the top record for each school.  Just like to table above.


I have tried a number of different things but no success.  

Thanks for the help.
0
Comment
Question by:lovettjay
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41863238
Can you given me the final output you need , I will write that with SQL Query itself.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41863473
Try using row_number and order that by the count descending
When that column rn is 1 it will be the top row you want
SELECT
      schools_name
    , members_exams_examdate
    , COUNT(me.schools_id)    AS numOfStudent
    , ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS rn
FROM members_exams AS me
JOIN schools AS s ON s.schools_id = me.schools_id
WHERE members_exams_examdate = '20160819'
GROUP BY
      schools_name
    , members_exams_examdate

Open in new window

By the way a date literal of yyyymmdd is THE safest to use in sql server.
Really don't recommend 2 digit years either

{+edit} please note I have changed this (edits via a phone just didn't work)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41863497
by the way...

row_number() over(order by count(*) DESC) as rn

will always result in a single rn = 1 (i.e. one row)
& you might can  include other "tie breaker" column(s) in the ordering

if there is an equal count of students over more than one school, then instead you coud use

rank() over(order by count(*) DESC) as rn

and the rn=1 would apply to more then one school (i.e. rank number of 1)

your call
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cs.Designer Issue(2) 2 23
Achieve json result 2 35
IO.FileSystemWatcher dies when used in Powershell 1 29
SQL Query 3 0
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now