简体   繁体   English

如何结合这两个LINQ语句?

[英]How to combine these two LINQ statements?

How can I combine these two LINQ statements into one? 如何将这两个LINQ语句合并为一个?

var Priority = repository.GetMany<Letter>(l => l.UserID == currentUser.ID)
                         .Select(l => l.Priority)
                         .FirstOrDefault();
var User = repository.GetMany<Letter>(l => l.Priority > Priority)
                     .Select(l => l.User)
                     .FirstOrDefault();

I need to get Priority of the currentUser and then get the next user that has the next Priority. 我需要获取currentUser的优先级,然后获取具有下一个Priority的下一个用户。 For example, if the Priority of currentUser is 1, I need to get the user with Priority == 2. 例如,如果currentUser的优先级为1,则需要获得优先级== 2的用户。

Example: 例:

letter=new letter { 
    User=Mark, Priority=1
    User=Raha, Priority=2
    User=Searah, Priority=3
}

when currentUser is Mark with Priority=1,i need to get user with Priority=2,in this sample Raha! 当currentUser是具有Priority = 1的Mark时,在此示例中,我需要获得Priority = 2的用户,Raha!

Assuming that your GetMany are indeed just Where calls, and therefore they returns an IQueryable, the following should do the trick I think: 假设您的GetMany确实只是Where调用,因此它们返回一个IQueryable,则下面的技巧应该可以解决:

var users = repository
    .GetMany<Letter>(l => l.Priority > (
        repository.GetMany<Letter>(
            l2 => l2.UserID = currentUser.ID
        )
        .Select(l2 => l2.Priority)
        .First()
    )
    .OrderBy(l => l.Priority)
    .Select(l => l.User)
    .Take(1);

Your first query is basically just inserted in the second query. 您的第一个查询基本上只是插入到第二个查询中。

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

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