简体   繁体   English

SharpRepository-在两个存储库之间联接

[英]SharpRepository - Join Between Two Repositories

I have scoured the net and was unable to find any example of conducting a join between two SharpRepository repos. 我已经在网上进行搜索,找不到任何在两个SharpRepository存储库之间进行联接的示例。 Can anyone provide a link to a page or an example? 任何人都可以提供指向页面或示例的链接吗? I am attempting to conver the following linq expression into a sharp repo expression: 我正在尝试将以下linq表达式转换为清晰的repo表达式:

        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;

----- UPDATE ------ -----更新------

This is what I came up with, but it doesn't seem to be working propertly... 这是我想出的,但似乎无法正常工作...

            var aur = new AccountUserRepository();
        var user = this.Join(aur, u => u.UserName.Contains(email), au => au.AccountId == accountId, 
            (u, au)=> u).AsQueryable().AsEnumerable();
        return user;

There is a Join method on the repository that is similar to a LINQ Join statement and will let you join one IRepository<> with another IRepository<>. 存储库上有一个Join方法,它与LINQ Join语句相似,可让您将一个IRepository <>与另一个IRepository <>连接。 You pass it an IRepository<> to join with, an inner key selector, an outer key selector and a result selector. 您将其传递给一个IRepository <>以与之结合,以使用内部键选择器,外部键选择器和结果选择器。

You can look here for an integration test that uses it: https://github.com/SharpRepository/SharpRepository/blob/master/SharpRepository.Tests.Integration/RepositoryJoinTests.cs 您可以在此处查找使用它的集成测试: https : //github.com/SharpRepository/SharpRepository/blob/master/SharpRepository.Tests.Integration/RepositoryJoinTests.cs

The result of this call is another repository that you can then call GetAll, or FindAll, etc. on just like it was a normal IRepository<> itself. 调用的结果是另一个存储库,然后您可以像正常的IRepository <>本身一样调用GetAll或FindAll等。 So I think you'll want to do something like this: 因此,我认为您想做这样的事情:

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);

That is how I think you would do it with the Join syntax. 我认为这就是使用Join语法实现的方式。

If you have navigation properties like you would in EF you could probably just do this syntax which is simpler: 如果您像EF中一样具有导航属性,则可能只需执行以下语法即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM