是否有一种更简单的方法来查询使用联接表的实体

发布于 2025-02-07 17:57:30 字数 426 浏览 2 评论 0原文

我正在尝试获取用户拥有的硬件,并且数据库具有“ *到 *”连接表,是否有办法使它变得更加简单?我不知道如何有。

        UserRecord = _userRepository.GetUserByID(userKey.UserId);
        var hjoiner = _hardwareUserRepository.GetHardwareUsers().Where(s => s.UserId == UserRecord.Id);

        foreach (var h in hjoiner) 
            _hardwareRepository.GetHardwares().FirstOrDefault(s => s.Id == h.HardwareId);
        

ps是的,我将重构所有桌子的存储库。

I'm trying to get Hardware a user owns and the database has a " * to * " joining table, is there a way to make this even more simple? I'm not aware of how to if there is.

        UserRecord = _userRepository.GetUserByID(userKey.UserId);
        var hjoiner = _hardwareUserRepository.GetHardwareUsers().Where(s => s.UserId == UserRecord.Id);

        foreach (var h in hjoiner) 
            _hardwareRepository.GetHardwares().FirstOrDefault(s => s.Id == h.HardwareId);
        

ps yes I am going to refactor things to not have a repository for every table.

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

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

发布评论

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

评论(1

与之呼应 2025-02-14 17:57:30

如果您的实体看起来像这样,

    public class HardwareUser
    {
        public int HardwareId { get; set; }
        public Hardware Hardware { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
    }

您可以如下查询

var hardwares = _hardwareUserRepository.GetHardwareUsers().Where(s => s.UserId == UserRecord.Id).Select(h=>h.Hardware).ToList();

If your entity looks like this

    public class HardwareUser
    {
        public int HardwareId { get; set; }
        public Hardware Hardware { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
    }

you can query as follows

var hardwares = _hardwareUserRepository.GetHardwareUsers().Where(s => s.UserId == UserRecord.Id).Select(h=>h.Hardware).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文