gpt4 book ai didi

mysql - Entity Framework Linq 查询为 MySQL 数据库返回不正确的值

转载 作者:行者123 更新时间:2023-11-30 23:27:04 25 4
gpt4 key购买 nike

我有一个问题:

    var cams = (from c in db.cameras
join s in db.camera_sites on c.camera_site_id equals s.id
join o in db.servers on s.server_id equals o.id
where (c.enabled == "yes"
&& s.enabled == "yes"
&& o.enabled == "yes"
&& o.type == "hosted"
&& o.server_method == "webservices"
&& c.account_id == accountId)
select new EventCamera_Named { Camera = c, SiteName = s.name, ServerName = o.name }).ToList();

问题是选择的 SiteName 与 Camera.name 相同

生成的sql是:

    SELECT
`Filter1`.`id`,
`Filter1`.`account_id`,
`Filter1`.`camera_site_id`,
`Filter1`.`enabled`,
`Filter1`.`name`,
...,
`Filter1`.`name` AS `name1`,
`Extent3`.`name` AS `name2`

FROM (SELECT
`Extent1`.`id`,
`Extent1`.`account_id`,
`Extent1`.`camera_site_id`,
`Extent1`.`enabled`,
`Extent1`.`name`,
...,
`Extent2`.`id` AS `id1`,
`Extent2`.`account_id` AS `account_id1`,
`Extent2`.`server_id`,
...
FROM `cameras` AS `Extent1` INNER JOIN `camera_sites` AS `Extent2` ON `Extent1`.`camera_site_id` = `Extent2`.`id`
WHERE ('yes' = `Extent1`.`enabled`) AND ('yes' = `Extent2`.`enabled`)) AS `Filter1` INNER JOIN `servers` AS `Extent3` ON `Filter1`.`server_id` = `Extent3`.`id`
WHERE ((('yes' = `Extent3`.`enabled`) AND ('hosted' = `Extent3`.`type`)) AND ('webservices' = `Extent3`.`server_method`)) AND (`Filter1`.`account_id` = 39884476)

我如何重写我的查询以返回正确的值以及导致它混淆的原因?

最佳答案

当我将 enabled 检查直接移到数据库之后时,它引用的是它生成的 sql 是正确的。

    var cams = (from c in db.cameras 
where c.enabled == "yes"
join s in db.camera_sites on c.camera_site_id equals s.id
where s.enabled == "yes"
join o in db.servers on s.server_id equals o.id
where (o.enabled == "yes"
&& o.type == "hosted"
&& o.server_method == "webservices"
&& c.account_id == accountId)
select new EventCamera_Named { Camera = c, SiteName = s.name, ServerName = o.name }).ToList();

关于mysql - Entity Framework Linq 查询为 MySQL 数据库返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com