如何使用Linq To Sql获取少于2张照片的用户?(How to use Linq To Sql to get Users who has less than 2 photos?)

编程入门 行业动态 更新时间:2024-10-22 15:39:18
如何使用Linq To Sql获取少于2张照片的用户?(How to use Linq To Sql to get Users who has less than 2 photos?)

场景是我想要获得少于2张照片的用户。

有两个表:

[Users] (UserId, UserName) [UserPhotos] (PhotoId, PhotoName, UserId)

UserId是外键,但我不想使用像user.Photos这样的关联。

用户可能在[UserPhotos]表中没有照片。

如何使用Linq To Sql获取少于2张照片的List<User> ?

The scenario is I want to get the users who has less than 2 photos.

There are two table:

[Users] (UserId, UserName) [UserPhotos] (PhotoId, PhotoName, UserId)

UserId is a Foreign Key but I do not want to use association like user.Photos.

A user may have none photo in the [UserPhotos] table.

How to use Linq To Sql to get List<User> who has less than 2 photos?

最满意答案

也许这个:( 修正版)

List<User> users = UserPhotos.GroupBy(i => i.UserId) .Where(i => i.Count() > 2).Distinct() .Join(Users, o => o.Key, i => i.UserId, (o, i) => i) .ToList();

改变问题的解决方案:

List<User> result = Users.Where(user => !UserPhotos.GroupBy(i => i.UserId) .Where(i => i.Count() >= 2).Distinct() .Any(i => i.Key == user.UserId)).ToList();

Maybe this: (fixed version)

List<User> users = UserPhotos.GroupBy(i => i.UserId) .Where(i => i.Count() > 2).Distinct() .Join(Users, o => o.Key, i => i.UserId, (o, i) => i) .ToList();

solution for changed question:

List<User> result = Users.Where(user => !UserPhotos.GroupBy(i => i.UserId) .Where(i => i.Count() >= 2).Distinct() .Any(i => i.Key == user.UserId)).ToList();

更多推荐

本文发布于:2023-07-31 02:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   照片   用户   Linq   photos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!