How to select MAX ID using LINQ? - c#

From the sql data below, I'd like to take a distinct EmpId that is the max ID.
ID EmpId DeptId
1 1002 XY
5 1100 ABC
6 1109 EF
7 1100 MN
9 1100 DE
10 1250 CE
11 1250 DJ
12 1100 DE
Results would look like the following:
ID EmpId DeptId
1 1002 XY
6 1109 EF
11 1250 DJ
12 1100 DE
How should this LINQ be structured?

var result = list.GroupBy(x=>x.EmpId).Select(g=>g.OrderByDescending(y=>y.Id).First());

from e in context.Employees
group e by e.EmpId into g
select new {EmpId = g.Key,
ID = g.OrderByDescending(gg=>gg.ID).FirstOrDefault().ID,
DeptId = g.OrderByDescending(gg=>gg.ID).FirstOrDefault().DeptId
}

Related

EF select many doesn't retrieve record which has no relation

I have two tables, for example, Student and Grade.
Those two tables have a relationship with many-to-many, so the table is StudentGrade.
By using the .SelectMany query, I can retrieve all records which have a relation.
For example,
var myResult = myDb.Student.SelectMany(x => x.Grade).ToList();
But let say I add a new record just to the Student table, which has no relation with Grade, this new record cannot be retrieved by using the query above.
How can I retrieve all data including this new one?
Student
Id Name Age
1 AAA 4
2 BBB 5
3 CCC 6
4 DDD 7
Grade
Id Name
1 G1
2 G2
3 G3
4 G4
StudentGrade
Student Grade
2 1
2 2
2 3
3 3
Require result:
Id Name Age Grade
1 AAA 4
2 BBB 5 G1
2 BBB 5 G2
2 BBB 5 G3
3 CCC 6 G3
4 DDD 7
I found full outer join would help but will it work in a many-to-many relationship?
How can I overcome this issue ?
var result = (from s in myDb.Student
from g in myDb.Grades
where !g.Select(x => x.Student).Contains(s) || g.Student == null || g.Student == s).ToList();
As long as I understand your question this should give you
all Students that have grades,
all Grades with no students,
and all Students with no grades.
I have not seen your models but this should help
var myResult = myDb.Student.SelectMany(x => x.Grade).ToList();
This query "means" retrieve all the Grades that have a Student. If you want the Students, select Student and Include the Grades if you want.
var myResult = myDb.Students.Include(x => x.Grades).ToList();

LINQ: Groupby and Last

I am new to LINQ.
I have the following table:
ID Field1 Field2 Field3
1 aaaa 20/01/2014 10
2 aaaa 21/01/2014 3
3 aaaa 25/01/2014 10
4 bbbb 01/01/2014 90
5 bbbb 03/01/2014 1
6 bbbb 31/01/2014 5
I want to group by Field1 and grab the last line of each group.
The SQL Query equivalent to this is:
SELECT Field1, Last(Field2) AS LastOfField2, Last(Field3) AS LastOfField3
FROM Table1
GROUP BY Field1
How this can be achieved on Linq?
var result = from p in Table1
group p by p.Field1 into grp
select grp.OrderByDescending(g=>g.Field2).First();

C# LINQ Join Error

SELECT PART_TYPE.PART_TYPE_ID,
PART_TYPE.PART_TYPE_NAME,
PART_AVAILABILITY.DATE_REF,
PART_TYPE.VEHICLE_ID,
PART_AVAILABILITY.AVAIL_COUNT
FROM PART_AVAILABILITY
RIGHT JOIN PART_TYPE
ON PART_AVAILABILITY.PART_TYPE_ID = PART_TYPE.PART_TYPE_ID
AND PART_AVAILABILITY.VEHICLE_ID = PART_TYPE.VEHICLE_ID
where PART_TYPE.VEHICLE_ID = 366
PART_TYPE_ID and VEHICLE_ID are Primary Key in PART_TYPE table.
VEHICLE_ID is a Foreign Key from VEHICLE table.
DATE_REF, PART_TYPE_ID and VEHICLE_ID are Primary Key in PART_AVAILABILITY table.
VEHICLE_ID and PART_TYPE_ID are Foreign Key from PART_TYPE table.
Above query gave below output.
PART_TYPE_ID PART_TYPE_NAME DATE_REF VEHICLE_ID AVAIL_COUNT
5 A1 2013-06-20 00:00:00.000 366 1
6 B1 2013-06-20 00:00:00.000 366 2
7 C1 2013-06-20 00:00:00.000 366 1
8 D1 NULL 366 NULL
9 E1 NULL 366 NULL
16 F1 2013-06-20 00:00:00.000 366 1
This my linq query for above sql query.
var vehiclePartType = from pa in context.PART_AVAILABILITY
join pt in context.PART_TYPE
on pa.PART_TYPE_ID equals pt.PART_TYPE_ID into joined
from j in joined.DefaultIfEmpty()
where j.VEHICLE_ID == 366
select new
{
PART_TYPE = j,
PART_AVAILABILITY = pa
};
But linq query gave below output.
PART_TYPE_ID PART_TYPE_NAME DATE_REF VEHICLE_ID AVAIL_COUNT
5 A1 2013-06-20 00:00:00.000 366 1
6 B1 2013-06-20 00:00:00.000 366 2
7 C1 2013-06-20 00:00:00.000 366 1
16 F1 2013-06-20 00:00:00.000 366 1
NULL records are missing.
How can i solve this ?
Right Join in LINQ is done by reversing join statements so correct one would be this:
var vehiclePartType = from pt in context.PART_TYPE
join pa in context.PART_AVAILABILITY on pt.PART_TYPE_ID equals pa.PART_TYPE_ID into joined
from j in joined.DefaultIfEmpty()
where pt.VEHICLE_ID == 366
select new
{
PART_TYPE = pt,
PART_AVAILABILITY = j
};
I think You have to swap your tables. Actually what you are doing is left join . To convert into right just swap the tables Like::
{
var vehiclePartType = from
pt in context.PART_TYPE join pa in context.PART_AVAILABILITY
on pt.PART_TYPE_ID equals pa.PART_TYPE_ID into joined
from j in joined.DefaultIfEmpty()
where j.VEHICLE_ID == 366
select new
{
PART_TYPE = j,
PART_AVAILABILITY = pa
};
}
I am not too much familier with LINQ but in your SQL query you have
AND PART_AVAILABILITY.VEHICLE_ID = PART_TYPE.VEHICLE_ID
This is missing in your LINQ query.

Getting the sum of quantity by product id

I'm struggling to get some results from my database. I have a table like this:
ID ID_INVOICE PRODUCT QUANTITY PRODUCT_ID
------------------------------------------
1 1 aaa 3 2
2 1 bbb 2 3
3 1 ccc 1 4
4 2 bbb 3 3
5 2 aaa 3 2
So after the query I would like to get something like
aaa 6
bbb 5
ccc 1
The query is based on the ID_INVOICE so far I've tried this:
SELECT product, sum(quantity)
FROM product
WHERE invoice_id = #p1
Add a GROUP BY product clause, like so:
SELECT product, sum(quantity)
FROM product
WHERE invoice_id = #p1
GROUP BY product;
SQL Fiddle Demo

linq sql where closest to number

i have a table
Id Number
1 9
2 10
3 12
4 19
5 20
select Id where Number is closest to 18
it should return row 4 which is 19
how do i write this in linq and tsql? thanks
(from q in table
orderby Math.Abs(18 - q.Number)
select q).FirstOrDefault()
and
SELECT TOP 1
*
FROM
table
ORDER BY
ABS(10 - Number)
and for a datetime
var nearTo = new DateTime(1999, 12, 31);
(from q in table
orderby Math.Abs((nearTo - q.Date).TotalSeconds)
select q).FirstOrDefault()

Categories

Resources