C# SharpRepository-连接两个存储库

C# SharpRepository-连接两个存储库,c#,linq,sharp-repository,C#,Linq,Sharp Repository,我在网上搜索过,找不到任何在两个存储库回购之间进行连接的示例。任何人都可以提供一个页面或示例的链接吗?我试图将以下linq表达式转换为夏普回购表达式: var user = (from f in _context.AccountUsers join h in _context.Users on f.UserId equals h.UserId where f.AccountId == accountI

我在网上搜索过,找不到任何在两个存储库回购之间进行连接的示例。任何人都可以提供一个页面或示例的链接吗?我试图将以下linq表达式转换为夏普回购表达式:

        var user = (from f in _context.AccountUsers
                    join h in _context.Users on f.UserId equals h.UserId
                    where f.AccountId == accountId && h.UserName.Contains(email)
                    select new
                    {
                        h
                    });
        return (IEnumerable<User>)user;

存储库中有一个类似于LINQ Join语句的Join方法,可以让您将一个IRepository与另一个IRepository连接起来。您将传递一个要联接的IRepository、一个内部关键点选择器、一个外部关键点选择器和一个结果选择器

您可以在此处查找使用它的集成测试:

这个调用的结果是另一个存储库,您可以在该存储库上调用GetAll或FindAll等,就像它本身是一个普通的IRepository一样。所以我想你会想这样做:

var accountUserRepo = new AccountUserRepository();
var userRepo = new UserRepository();

// join on the UserId column that is common to both, and select an anonymous type with both pieces of info (you would select just what you need)
var compositeRepo = accountUserRepo.Join(userRepo, au => au.UserId, u => u.UserId, (au, u) => new { AccountUserInfo = au, UserInfo = u } );

return compositeRepo.FindAll(x => UserInfo.UserName.Contains(email) && x.AccountInfo.AccountId == accountId, x => x.UserInfo);
这就是我认为使用Join语法的方法

如果您有像EF中一样的导航属性,您可能只需要执行以下更简单的语法:

return accountUserRepo.FindAll(x => x.AccountId == accountId && x.User.UserName.Contains(email), x => x.User);

顺便说一句,sharprepo版本中的这个是UserAccount(用户)repo。感谢您的帮助,我没有意识到它返回了另一个repo!是的,我想我们可以把文档做得更好一些。我会记下来的。Jeff,你能解释一下如何进行外部连接吗?不幸的是,对于外部连接,你需要突破存储库,获得IQueryable,然后像对付LINQ那样进行操作。您可以调用userRepo.AsQueryable()来获取底层的queryable,然后可以像平常一样使用它。请记住,当您这样做时,您将无法获得缓存的好处,也无法获得存储库本身可能具有的任何方面/挂钩。
return accountUserRepo.FindAll(x => x.AccountId == accountId && x.User.UserName.Contains(email), x => x.User);