LINQ left join with only the row having maximum value of a column

Taha Rehman Siddiqui

I have a table ProjectInformation as (left table)

ProjectID int
{fields}

the other table, ProjectUpdates (right table). This table has multiple record for 1 ProjectID added monthly.

ProjectID int
CreateDate date
{other fields that have records}

Their relation is 1-M. Sample Data

ProjectInformation
{ ProjectID = 1, DataA = "ABC"}
{ ProjectID = 2, DataA = "DEF"}
{ ProjectID = 3, DataA = "GHI"}

ProjectUpdates
{ProjectID = 1, CreateDate = "24/2/2014", DataB = "JKL"}
{ProjectID = 1, CreateDate = "25/1/2014", DataB = "MNL"}
{ProjectID = 1, CreateDate = "23/12/2014", DataB = "PQR"}
{ProjectID = 1, CreateDate = "23/11/2014", DataB = "STU"}
{ProjectID = 2, CreateDate = "24/2/2014", DataB = "VWX"}
{ProjectID = 2, CreateDate = "24/1/2014", DataB = "YZA"}
{ProjectID = 3, CreateDate = "21/12/2014", DataB = "BCD"}
{ProjectID = 3, CreateDate = "24/11/2014", DataB = "EFG"}
{ProjectID = 3, CreateDate = "24/10/2014", DataB = "HIJ"}
{ProjectID = 3, CreateDate = "24/8/2014", DataB = "KLM"}
{ProjectID = 3, CreateDate = "24/6/2014", DataB = "NOP"}

I want my LINQ query to return the following Data (1 row for each project)

Criteria: The row in left table will join with the one in the right which has the largest value of CreateDate for the project.

{ProjectID = 1, CreateDate = "24/2/2014", DataA ="ABC", DataB = "JKL"}
{ProjectID = 2, CreateDate = "24/2/2014", DataA ="DEF", DataB = "VWX"}
{ProjectID = 3, CreateDate = "21/12/2014", DataA ="GHI", DataB = "BCD"}
Giorgi Nakeuri

You can do it many ways. One of those way is:

var result = (from pi in projectInformations
              join pu in projectUpdates on pi.ProjectID equals pu.ProjectID into tpu
              from t in tpu.OrderByDescending(c => c.CreateDate).Take(1)
              select new { pi.ProjectID, pi.DataA, t.CreateDate, t.DataB }).ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get ID of a row having maximum value in other column

From Dev

MySQL LEFT JOIN only one row, ordered by column without subquery

From Dev

MySQL LEFT JOIN only one row, ordered by column without subquery

From Dev

Return one row in a view based on a left join column value

From Dev

SQL LEFT JOIN first row only

From Dev

Return Only Last Row LEFT JOIN

From Dev

LEFT JOIN with self table only first row

From Dev

How to perform left join in linq having multiple table joins

From Dev

LINQ to Sql Left Outer Join with Group By and Having Clause

From Dev

How to use constant value in Linq left join?

From Dev

How to use constant value in Linq left join?

From Dev

How to make LEFT JOIN with row having max date?

From Dev

Select distinct on Left Join for only one column

From Dev

left join tables, use only one row from left table

From Dev

CSS: Advanced row layout with one column having a relative maximum width

From Dev

Left join repeats same value of a column in result

From Dev

Getting the column name that holds the maximum value of a row

From Dev

Finding maximum value in a column and return row number

From Dev

Column index with row-wise maximum value

From Dev

How to get the row with the maximum value of a summed column

From Dev

Find row and column index of maximum value in a matrix

From Dev

SQLite Maximum matching row by multi column with value

From Dev

SQL SELECT in JOIN last (maximum) row before the row with specific value

From Dev

LINQ query join only last value

From Dev

LINQ query join only last value

From Dev

LINQ Left join on nullable column giving null reference exception

From Dev

how to convert column to row in mysql(column having the same value)

From Dev

Mysql: Join Table Only First row on left table

From Dev

SQL left join only returns one row instead of many

Related Related

  1. 1

    Get ID of a row having maximum value in other column

  2. 2

    MySQL LEFT JOIN only one row, ordered by column without subquery

  3. 3

    MySQL LEFT JOIN only one row, ordered by column without subquery

  4. 4

    Return one row in a view based on a left join column value

  5. 5

    SQL LEFT JOIN first row only

  6. 6

    Return Only Last Row LEFT JOIN

  7. 7

    LEFT JOIN with self table only first row

  8. 8

    How to perform left join in linq having multiple table joins

  9. 9

    LINQ to Sql Left Outer Join with Group By and Having Clause

  10. 10

    How to use constant value in Linq left join?

  11. 11

    How to use constant value in Linq left join?

  12. 12

    How to make LEFT JOIN with row having max date?

  13. 13

    Select distinct on Left Join for only one column

  14. 14

    left join tables, use only one row from left table

  15. 15

    CSS: Advanced row layout with one column having a relative maximum width

  16. 16

    Left join repeats same value of a column in result

  17. 17

    Getting the column name that holds the maximum value of a row

  18. 18

    Finding maximum value in a column and return row number

  19. 19

    Column index with row-wise maximum value

  20. 20

    How to get the row with the maximum value of a summed column

  21. 21

    Find row and column index of maximum value in a matrix

  22. 22

    SQLite Maximum matching row by multi column with value

  23. 23

    SQL SELECT in JOIN last (maximum) row before the row with specific value

  24. 24

    LINQ query join only last value

  25. 25

    LINQ query join only last value

  26. 26

    LINQ Left join on nullable column giving null reference exception

  27. 27

    how to convert column to row in mysql(column having the same value)

  28. 28

    Mysql: Join Table Only First row on left table

  29. 29

    SQL left join only returns one row instead of many

HotTag

Archive