O
O
okbuddy2021-08-02 16:46:07
.NET
okbuddy, 2021-08-02 16:46:07

How to relay SQL statement data?

I wrote two such SQL statements, and when I tried to translate them into LINQ, I realized that I was a little weak in LINQ
1.

SELECT Project.name, Test.name
FROM Project
JOIN Test ON Test.project_id = Project.id
WHERE(Test.project_id = Project.id) AND (Test.start_time > '2015-11-07 00:00:00')
ORDER BY Project.name, Test.name ASC

2.
SELECT SUM(if(Test.browser = 'chrome', 1, 0)) AS Count
FROM Test
UNION SELECT SUM(if(Test.browser = 'firefox', 1, 0)) AS Count FROM Test

I tried to write something like LINQ to the first query, this is what happened
var query = db.Projects.Join(db.Tests,
                    p => p.Id,
                    t => t.ProjectId,
                    (p, t) => new
                    {
                        Project = p.Name,
                        Test = db.Tests.Where(t => t.ProjectId == p.Id).FirstOrDefault(t => t.StartTime > new DateTime(2015, 11, 7)).Name
                    });

It came out briefly saying nothing, because FirstOrDefault displays only 1 record for each project, but I need to display several tests for each project, and I have no idea how to display the names of the tests through Select, since it returns IQueryableand in Runtime it throws an exception
Here what query forms LINQ
SELECT `p`.`name` AS `Project`, (
    SELECT `t`.`name`
    FROM `test` AS `t`
    WHERE (`t`.`project_id` = `p`.`id`) AND (`t`.`start_time` > '2015-11-07 00:00:00')
    LIMIT 1) AS `Test`
FROM `project` AS `p`
INNER JOIN `test` AS `t0` ON `p`.`id` = `t0`.`project_id`

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
V Sh., 2021-08-02
@JuniorNoobie

Do you breed by budding there? Or is it some kind of common task?
There is an implementation like this, for example: the same question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question