使用LinqWhy执行左外部联接时获得错误结果
本文关键字:错误 结果 LinqWhy 执行 外部 使用 | 更新日期: 2023-09-27 18:24:57
我有以下
public class Person
{
// Properties
public string Name { get; set; }
public string Area { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
public List<Person> GetPersonData()
{
List<Person> personLst = new List<Person> {
new Person { Name="Shashidhar Niketani", Age=20, Gender="Male" , Area = "Assam"},
new Person { Name="Ahmed Ali Khan", Age=25 ,Gender="Male", Area = "Assam" },
new Person { Name="S. Mirja", Age=20, Gender="Female", Area = "Assam"},
new Person { Name="Neru Kumar", Age=18, Gender="Female", Area = "Colombo"},
new Person { Name="Chidam P", Age=19, Gender="Male", Area = "Colombo"},
new Person { Name="H Kontala", Age=19, Gender="Male", Area = "Bombay"},
new Person { Name="Priya Pankhraj", Age=23, Gender="Female", Area = "North Punjab"},
new Person { Name="Ambla", Age=20, Gender="Female", Area = "Madras"},
new Person { Name="H Kontala", Age=25, Gender="Male", Area = "Bombay"},
new Person { Name="Sirisha Chalukuri", Age=30, Gender="Female", Area = "Bombay"}};
return personLst;
}
public override string ToString()
{
return string.Format("Patient Name: = {0} , Age:= {1}, Gender:= {2}, Area: = {3}", Name, Age, Gender, Area);
}
}
public class Hobbies
{
public string Owner { get; set; }
public string HobbyName { get; set; }
public List<Hobbies> GetHobbies()
{
List<Hobbies> hobbyList = new List<Hobbies> {
new Hobbies { Owner="Sirisha Chalukuri", HobbyName = "Singing"},
new Hobbies { Owner="Priya Pankhraj", HobbyName = "Cooking" },
new Hobbies { Owner="S. Mirja", HobbyName="Playing"},
new Hobbies { Owner="Neru Kumar", HobbyName="Programing"}};
return hobbyList;
}
}
我正在尝试执行左加入
//提取那些记录
Person p = new Person();
var personSource = p.GetPersonData();
Hobbies h = new Hobbies();
var hobbySource = h.GetHobbies();
//左侧外部连接
var res8 = (from person in personSource
join hobby in hobbySource
on p.Name equals hobby.Owner into temp
from hobby in temp.DefaultIfEmpty()
select new
{
PersonName = p.Name,
PersonAge = p.Age,
Gender = p.Gender,
LivesIn = p.Area,
Hobby = (hobby == null) ? "N/A" : hobby.HobbyName
});
但是我不能得到正确的结果。。。所有记录都是空/不匹配的记录。。至少有4条匹配记录,其余将是不匹配记录。。。
我想找的是,给定这两个数据源,如果我们对PersonName和HobbyOwner执行左联接,我们会期望得到的结果。
需要的帮助
当你提到业余爱好时。HobbyName在您选择的投影中,当一个人没有匹配的爱好时,hobby为null(对于空的爱好集合,DefaultIfEmpty返回null)。您需要检查hobby是否为null,如果为null,则为选择投影中的hobby属性指定一个默认值。例如:
select new
{
PersonName = p.Name,
PersonAge = p.Age,
Gender = p.Gender,
LivesIn = p.Area,
Hobby = (hobby == null) ? "N/A" : hobby.HobbyName
});
此外,不知道这是否是拼写错误,但在您的查询中,当您应该使用person:时,您使用了p
var res8 = (from person in personSource
join hobby in hobbySource
on person.Name equals hobby.Owner into temp
from hobby in temp.DefaultIfEmpty()
select new
{
PersonName = person.Name,
PersonAge = person.Age,
Gender = person.Gender,
LivesIn = person.Area,
Hobby = (hobby == null) ? "N/A" : hobby.HobbyName
});
根据定义,左外部联接将从第一个集合中获取所有记录,而不管右集合中是否存在匹配的记录。
我想你要找的是:
var res8 = (from person in personSource
join hobby in hobbySource
on p.Name equals hobby.Owner into temp
from hobby in temp.DefaultIfEmpty()
select new
{
PersonName = p.Name,
PersonAge = p.Age,
Gender = p.Gender,
LivesIn = p.Area,
Hobby = hobby == null ? null : hobby.HobbyName
});
我对您的代码所做的唯一一件事就是在您的最后一个块(以及我在下面复制、修改和粘贴的最后一块)中更改p
到person
的每一次出现:
var res8 = (from person in personSource
join hobby in hobbySource
on person.Name equals hobby.Owner into temp
from hobby in temp.DefaultIfEmpty()
select new {
PersonName = person.Name,
PersonAge = person.Age,
Gender = person.Gender,
LivesIn = person.Area,
Hobby = (hobby == null) ? "N/A" : hobby.HobbyName
});
此外,我不知道这段代码与实际代码有多相似,但您的GetPersonData()和GetHobbies()函数作为静态方法更有意义。这样,你就不需要创建一个无用的个人或爱好实例来获得你的列表。看起来上面的p
Person实例让你失望了。