carol1287
carol1287

Reputation: 395

Convert linq to SQL

Does anyone know how to translate this? When I hit the debugger I get 3 different queries and var contains an array of results. I am trying to replace that line with a method that will call a stored procedure but I do not understand what the query should be. Thanks a lot

var restbl =
        context.tbl_one.FirstOrDefault(d => d.qty < d.tbl_two.Count(a => !a.tbl_three.ust))
     ?? context.tbl_one.FirstOrDefault(d => d.qty > d.tbl_two.Count(a => !a.tbl_three.ust));


{SELECT
`Extent1`.`id`, 
`Extent1`.`name`, 
`Extent1`.`qty`, 
`Extent1`.`cdate`
FROM `tbl_one` AS `Extent1`}

{SELECT
`Extent1`.`id`, 
`Extent1`.`tbl_one_id`, 
`Extent1`.`tbl_three_id`, 
`Extent1`.`enabled`
FROM `tbl_two` AS `Extent1`}

{SELECT
`Extent1`.`id`, 
`Extent1`.`ttid`, 
`Extent1`.`code`, 
`Extent1`.`cdate`, 
`Extent1`.`mdate`, 
`Extent1`.`prt`, 
`Extent1`.`ust`
FROM `tbl_three` AS `Extent1`}

var countToAdd = restbl.qty - context.tbl_two.Count(a => a.tbl_one_id == restbl.id && !a.tbl_three.ust);

Upvotes: 0

Views: 295

Answers (2)

Ole Albers
Ole Albers

Reputation: 9285

My assumption would be: (assuming you use MSSQL)

result= SELECT TOP 1 FROM tbl_one t1 WHERE t1.qty< (SELECT COUNT(*) FROM tbl_two t2 INNER JOIN tbl_three t3 ON t3.id=t2.tbl_three_id WHERE t3.ust = 0 AND t1.id=t2.tbl_one-id)

if (result IS NULL)
result= SELECT TOP 1 FROM tbl_one t1 WHERE t1.qty> (SELECT COUNT(*) FROM tbl_two t2 INNER JOIN tbl_three t3 ON t3.id=t2.tbl_three_id WHERE t3.ust = 0 AND t1.id=t2.tbl_one-id)

Upvotes: 0

vrharilal
vrharilal

Reputation: 126

You can use LINQPad or Linquer tool for easily converting your linq query to SQL or vice versa. They are very helpful in converting complex queries.

Upvotes: 1

Related Questions