C#中,当满足条件时,如何从谓词列表中获取属性。

huangapple go评论64阅读模式
英文:

C# linq how to get property from predicate list when condition is met

问题

以下是您提供的代码的中文翻译:

我有以下的linq查询

var usersToNotify = trainingUsers.Where(x => delegatesToBeReminded.Any(d => d.UserGuid == x.UserGuid))
                                 .Select(x => new RecipientDetail
                                 {
                                     FullName = x.FullName,
                                     Email = x.Email,
                                     // 从delegatesToBeReminded获取属性
                                 })
                                 .ToList();

在上面的示例中,我有trainingusers和delegatesToBeReminded列表。我想要检索在trainingusers中找到的匹配记录,并创建自定义类型,包括trainingusers中的fullname和email以及delegatesToBeReminded中的附加属性。

有人能帮我如何做吗?

我可以使用类似这样的代码吗?

var x = from tu in trainingUsers
        join d in delegatesToBeReminded on tu.UserGuid equals d.UserGuid
        select new RecipientDetail
        {
            FullName = tu.FullName,
            Email = tu.Email,
            Session = d.Session
        };

谢谢

英文:

I have the following linq query

var usersToNotify = trainingUsers.Where(x => delegatesToBeReminded.Any(d => d.UserGuid == x.UserGuid))
                                 .Select(x => new RecipientDetail
                                 {
                                     FullName = x.FullName,
                                     Email = x.Email,
// get property from delegatesToBeReminded
                                 })
                                 .ToList();

In the example above, i have trainingusers and delegatesToBeReminded list. i want to retrieve the matching record found in trainingusers and create custom type, with fullname, email from trainingusers and additional property from delegatesTobeReminded.

Can anyone help me how to do this?

Can i use something like this?

            var x = from tu in trainingUsers
                    join d in delegatesToBeReminded on tu.UserGuid equals d.UserGuid
                    select new RecipientDetail
                    {
                        FullName = tu.FullName,
                        Email = tu.Email,
                        Session = d.Session
                    };

Thanks

答案1

得分: 2

以下是您要翻译的代码部分:

Easiest would be to use a join, as you suggested: 

trainingUsers.Join(
    delegatesToBeReminded,
    user => user.UserGuid,
    delegateToBeReminded => delegateToBeReminded.UserGuid,
    (user, delegateToBeReminded) => new RecipientDetail
    {
        FullName = user.FullName,
        Email = user.Email,
        Delegate = delegateToBeReminded,
    });

(Or you can write the equivalent in linq query syntax, as you did).

Another way is to rewrite this in linq query syntax, using let:

from user in trainingUsers
let delegateToBeReminded = delegatesToBeReminded.FirstOrDefault(d => d.UserGuid == user.UserGuid)
where delegateToBeReminded != null
select new RecipientDetail
{
    FullName = user.FullName,
    Email = user.Email,
    Delegate = delegateToBeReminded,
}

Note that these differ depending on what happens if there is more than one delegate for a particular user. The first creates a new RecipientDetail object for each user/delegate pair; the second creates a RecipientDetail object per user, and picks the first delegate.

英文:

Easiest would be to use a join, as you suggested:

trainingUsers.Join(
    delegatesToBeReminded,
    user => user.UserGuid,
    delegateToBeReminded => delegateToBeReminded.UserGuid,
    (user, delegateToBeReminded) => new RecipientDetail
    {
        FullName = user.FullName,
        Email = user.Email,
        Delegate = delegateToBeReminded,
    });

(Or you can write the equivalent in linq query syntax, as you did).

Another way is to rewrite this in linq query syntax, using let:

from user in trainingUsers
let delegateToBeReminded = delegatesToBeReminded.FirstOrDefault(d => d.UserGuid == user.UserGuid)
where delegateToBeReminded != null
select new RecipientDetail
{
    FullName = user.FullName,
    Email = user.Email,
    Delegate = delegateToBeReminded,
}

Note that these differ depending on what happens if there is more than one delegate for a particular user. The first creates a new RecipientDetail object for each user/delegate pair; the second creates a RecipientDetail object per user, and picks the first delegate.

huangapple
  • 本文由 发表于 2020年1月6日 23:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定