I am trying to join two objects in linq as below:
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.
Expected: When personList is null, we are still supposed to receive the clients result but assigning ip to be '-' when no result in personList.
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:
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:
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:
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 .
--- Output (Console Application) ---
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 ---
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}");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.