How do I transfer this SQL statement into a LINQ query?

Bautzi89

I have this pretty easy SQL Statement and I want to get the same data using LINQ, but I can't seem to find the right way.

select A.AUFTRAGID,
       A.AUFTRAGNR, 
       A.GESELLSCHAFTID, 
       A.DEBITORID,
       A.DEBITOR_KOMMNR2,
       A.DEBITOR_TEILELIEFERNR, 
       D.DEPOTID, 
       D.DEPOTNUM, 
       D.MATCHCODE, 
       D.NAME, 
       D.KS_ID
from AUFTRAG A
join DEPOT D on D.DEPOTID = A.DEPOTID and D.VALID = 1 and D.KS_ID = 1
where A.AUFTRAGID in 
      (select AUFTRAGID from AUFTRAG_STATUS where VALID = 1 
           and CRTI = (select max(CRTI) from AUFTRAG_STATUS where AUFTRAGID = A.AUFTRAGID) 
           and [STATUS] = 9)

I can successfully join the tables [AUFTRAG] and [DEPOT], but when it comes to the last where clause I am not able to figure out how to get my data in LINQ.

I'm looking forward for your help.

UPDATE - This is what I have done so far:

var erfAuftr = (from auf in db.AUFTRAG
                join dep in (from dep in db.DEPOT
                             where dep.KS_ID == 1
                             select dep) on auf.DEPOTID equals dep.DEPOTID
                join a_s in
                     (from a_s in db.AUFTRAG_STATUS
                      group a_s by new
                      {
                           a_s.AUFTRAGID
                      } into grp
                      select new
                      {
                           AuftragId = grp.Key.AUFTRAGID,
                           Date = grp.Max(s => s.CRTI)
                      }) on auf.AUFTRAGID equals a_s.AuftragId                                   
                 select new
                 {
                      AuftragId = auf.AUFTRAGID,
                      AuftragNr = auf.AUFTRAGNR,
                      DebitorId = auf.DEBITORID,
                      KVNr = auf.DEBITOR_KOMMNR2,
                      TL = auf.DEBITOR_TEILELIEFERNR,
                      DepotId = dep.DEPOTID,
                      DepotNum = dep.DEPOTNUM,
                      DepotMatchcode = dep.MATCHCODE,
                      DepName = dep.NAME1,
                      WEDate = a_s.Date
                  });

But I am still missing how to check for [STATUS] = 9

Bautzi89

I finally got it working by using the following expression:

Date = grp.Where(g => g.CRTI == grp.Max(s => s.CRTI) && g.STATUS == 9).Max(s => s.CRTI)

Thank you for your help anyway!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get my LINQ Query to generate the SQL statement I expect?

From Dev

In Azure DocumentDb, how do I write an "IN" statement using LINQ to SQL

From Dev

How do I run an SQL update query using a like statement

From Dev

How do I declare a query in an oracle pl/sql statement?

From Dev

How do I run an SQL update query using a like statement

From Dev

How do I get the count for a SQL query containing a GROUP BY statement?

From Dev

how do I change this sql query [Select MAX(id)] to linq

From Dev

How do I rewrite this SQL query in LINQ format?

From Dev

How do I rewrite this SQL query in LINQ format?

From Dev

How do I pass a parameter from a LINQ query into a SQL View?

From Dev

How do I sort the results of a LINQ to SQL query using approximation?

From Dev

how i write this sql query to linq query

From Dev

how i write this sql query to linq query

From Dev

How do I convert this tSQL statement to LINQ using group by in a sub query

From Dev

C# How do I use a variable in a Linq select statement using Query Syntax

From Dev

convert sql query to LINQ statement

From Dev

how do I make this LINQ query faster?

From Dev

How do I write this linq inner query?

From Dev

How do I write this linq inner query?

From Dev

How do I nest this LINQ query?

From Dev

How can I do this LINQ query in BreezeJS?

From Dev

How do I convert this query into LINQ

From Dev

How do I order by a function in LINQ query?

From Dev

How can I convert this SQL query to LINQ

From Dev

How do i do the "IN" and "DISTINCT of SQL to LINQ

From Dev

Transfer Odata query to SQL statement java

From Dev

how could i handle this linq query and compare it with and int in an if-statement?

From Dev

how could i handle this linq query and compare it with and int in an if-statement?

From Dev

(SQL) In a query, how do I use a CASE statement that would only conditionally require a JOIN?

Related Related

  1. 1

    How do I get my LINQ Query to generate the SQL statement I expect?

  2. 2

    In Azure DocumentDb, how do I write an "IN" statement using LINQ to SQL

  3. 3

    How do I run an SQL update query using a like statement

  4. 4

    How do I declare a query in an oracle pl/sql statement?

  5. 5

    How do I run an SQL update query using a like statement

  6. 6

    How do I get the count for a SQL query containing a GROUP BY statement?

  7. 7

    how do I change this sql query [Select MAX(id)] to linq

  8. 8

    How do I rewrite this SQL query in LINQ format?

  9. 9

    How do I rewrite this SQL query in LINQ format?

  10. 10

    How do I pass a parameter from a LINQ query into a SQL View?

  11. 11

    How do I sort the results of a LINQ to SQL query using approximation?

  12. 12

    how i write this sql query to linq query

  13. 13

    how i write this sql query to linq query

  14. 14

    How do I convert this tSQL statement to LINQ using group by in a sub query

  15. 15

    C# How do I use a variable in a Linq select statement using Query Syntax

  16. 16

    convert sql query to LINQ statement

  17. 17

    how do I make this LINQ query faster?

  18. 18

    How do I write this linq inner query?

  19. 19

    How do I write this linq inner query?

  20. 20

    How do I nest this LINQ query?

  21. 21

    How can I do this LINQ query in BreezeJS?

  22. 22

    How do I convert this query into LINQ

  23. 23

    How do I order by a function in LINQ query?

  24. 24

    How can I convert this SQL query to LINQ

  25. 25

    How do i do the "IN" and "DISTINCT of SQL to LINQ

  26. 26

    Transfer Odata query to SQL statement java

  27. 27

    how could i handle this linq query and compare it with and int in an if-statement?

  28. 28

    how could i handle this linq query and compare it with and int in an if-statement?

  29. 29

    (SQL) In a query, how do I use a CASE statement that would only conditionally require a JOIN?

HotTag

Archive