我怎样才能做到“定位非重合”?在 Linq 中查询?

发布于 2024-10-28 03:35:58 字数 154 浏览 1 评论 0原文

假设我有一个表“available_cars”[car_id,descritpion]和另一个表“cars_x_client”[client_id,car_id]。如何查询客户没有的所有车辆?

我知道用 SQL 来做。也许这是小菜一碟,但我不知道如何在 Linq 中做到这一点。

Supose I have a table "available_cars" [car_id, descritpion] and another table "cars_x_client" [client_id, car_id]. How can I query all the cars that the client doesn't have?

I know do it in SQL. Maybe it is a piece of cake, but I don't know how to do it in Linq.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-11-04 03:35:58

为了有效地做到这一点,您可以使用联接 - 这将被转换为 SQL 中的内部联接:

var query = from available in available_cars
    join client_car in cars_x_client on available.car_id equals client_car.car_id
    where client_car.client_id != my_client_id
    select car;

还有可用于外部联接的机制 - 例如,如果有一辆不属于任何客户端的汽车,则使用类似以下内容

var query = from available in available_cars
    join client_car in cars_x_client on available.car_id equals client_car.car_id into outer_join
    from outer in outer_join.DefaultIfEmpty
    where (outer == null || outer.client_id != my_client_id)
    select car;

:关于 linq,包括联接,我推荐 101 个 linq 示例 - http://msdn.microsoft.com /en-us/vcsharp/aa336746.aspx

To do this efficiently you can use a join - this will be translated to an inner join in SQL:

var query = from available in available_cars
    join client_car in cars_x_client on available.car_id equals client_car.car_id
    where client_car.client_id != my_client_id
    select car;

There are also mechanisms available for outer joins - e.g. if there's a car which doesn't belong to any client then use something like:

var query = from available in available_cars
    join client_car in cars_x_client on available.car_id equals client_car.car_id into outer_join
    from outer in outer_join.DefaultIfEmpty
    where (outer == null || outer.client_id != my_client_id)
    select car;

For more on linq including joins I recommend 101 linq examples - http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

ζ澈沫 2024-11-04 03:35:58

尝试一下这个。

from car in available_cars
where !cars_x_client.Any(x => x.client_id == client_id 
                         && x.car_id == car.car_id)
select car;

Try something this.

from car in available_cars
where !cars_x_client.Any(x => x.client_id == client_id 
                         && x.car_id == car.car_id)
select car;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文