简体   繁体   English

在 linq 中合并为等于

[英]coalescing in linq join for equals

I am trying to join two objects in linq as below:我正在尝试在 linq 中加入两个对象,如下所示:

var clients = from cts in ctList
              join pc in personList on ct.name equals pc.name
              select new 
              {
                ip = pc.Ip,
                clts = cts
              };

Actual Result: I am getting clients with no result when personList is null.实际结果:当 personList 为 null 时,我得到的客户没有结果。

Expected: When personList is null, we are still supposed to receive the clients result but assigning ip to be '-' when no result in personList.预期:当 personList 为 null 时,我们仍然应该接收客户端结果,但是当 personList 中没有结果时,将 ip 分配为“-”。

I want to achieve like below but it is not working:我想实现如下,但它不起作用:

join pc in personList on ct.name equals pc?.name ?? ct.name

I would really appreciate for the help.我非常感谢您的帮助。 Thank you!谢谢!

By using left join, the query will be:通过使用左连接,查询将是:

var clients = from cts in ctList
              join pc in personList on cts.name equals pc.name into lefedResults
              from lefedResult in lefedResults.DefaultIfEmpty()
              select new 
              {
                ip = lefedResult?.Ip ?? "-",
                clts = cts
              };

I hope you find this helpful.我希望你觉得这有帮助。

Solution 1:解决方案1:

var clients = from cts in ctList
    from pc in personList.Where(x => x.name == cts.name).DefaultIfEmpty()
    select new
    {
        ip = pc?.Ip ?? "-",
        clts = cts
    };

Solution 2:解决方案2:

var clients = from cts in ctList
              select new
              {
                  ip = personList.Where(x => x.name == cts.name).Select(x => x.Ip).FirstOrDefault() ?? "-",
                  clts = cts
              };

Solution 3:解决方案3:

var clients = ctList.Select(cts => new
{
    clts = cts,
    ip = personList.Where(x => x.name == cts.name).Select(x => x.Ip).DefaultIfEmpty("-").First()
});

Find an example below with minor changes to increase robustness assuming the input list may not be sanitized:假设输入列表可能未经过清理,请在下面找到一个稍作更改以提高稳健性的示例:

More details available at https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins .更多详细信息,请访问https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins

--- Output (Console Application) --- --- Output(控制台应用程序)---

John -> 10.0.0.5
Sally -> 10.0.0.7
Sally -> 10.0.0.9
Sally -> -
<Unknown User> -> 10.1.1.100
<Unknown User> -> -
<Unknown User> -> -

--- Classes --- --- 课程 ---

class Person
{
    public string Name { get; set; }
}

class Client
{
    public string OwnerName { get; set; }

    public string Ip { get; set; }
}

--- LINQ Query --- --- LINQ 查询 ---

        List<Person> people = new List<Person>{
            new Person { Name = "John" },
            new Person { Name = "Mary" },
            new Person { Name = "Sally" },
            new Person { Name = null },
            null
        };

        List<Client> machines = new List<Client>{
            new Client { OwnerName = "John", Ip = "10.0.0.5" },
            new Client { OwnerName = "Sally", Ip = "10.0.0.7" },
            new Client { OwnerName = "Sally", Ip = "10.0.0.9" },
            new Client { OwnerName = "Sally", Ip = null }, // Sally -> -
            new Client { OwnerName = null, Ip = "10.1.1.100" }, // <Unknown User> -> 10.1.1.100
            new Client { OwnerName = null, Ip = null }, // <Unknown User> -> -
            null // <Unknown User> -> -
        };

        if (people != null && machines != null)
        {
            var query = from machine in machines
                        join person in people on machine?.OwnerName equals person?.Name into gj
                        select new { Name = machine?.OwnerName?? "<Unknown User>", Ip = machine?.Ip ?? "-" };

            foreach (var result in query)
            {
                Console.WriteLine($"{result.Name} -> {result.Ip}");
            }
        }

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

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