外连接中的内部异常
本文关键字:内部 异常 连接 | 更新日期: 2023-09-27 18:13:44
我不知道为什么我的程序不工作。你能帮帮我吗?当我运行它时,我有一个内部异常。
var peoples = new List<People>();
peoples.Add(new People { Model = "Mustang", FirstName = "Joe", LastName = "L'indien" });
peoples.Add(new People { Model = "Corvet", FirstName = "Luc", LastName = "Emince" });
peoples.Add(new People { Model = "Verron", FirstName = "pierre", LastName = "Uneed" });
peoples.Add(new People { Model = "Viper", FirstName = "Martial", LastName = "Roste" });
peoples.Add(new People { Model = "Camaro", FirstName = "Franck", LastName = "Vador" });
var cars = this._cars = new List<Car>();
cars.Add(new Car { Model = "Mustang", Description = "Gros paté" });
cars.Add(new Car { Model = "Mustang", Description = "Paté" });
cars.Add(new Car { Model = "Verron", Description = "Très gros paté" });
cars.Add(new Car { Model = "Viper", Description = "Attention paté" });
cars.Add(new Car { Model = "Viper", Description = "Attention paté" });
var PeopleCar = from people in peoples
join car in cars on people.Model equals car.Model into outer
from car in outer.DefaultIfEmpty()
select new
{
people.FirstName,
people.LastName,
car.Model,
Desc = (people == null) ? "CASSE DE MERDE" : car.Description
};
foreach (var item in PeopleCar)
{
textBox1.AppendText(String.Format("Mr {0} {1} a une voiture model {2} qui envoie du {3}"
+ Environment.NewLine, item.FirstName, item.LastName, item.Model, item.Desc));
}
在select中定义字段名:
select new
{
FirstName = people.FirstName,
LastName = people.LastName,
Model = car.Model,
Desc = (people == null) ? "CASSE DE MERDE" : car.Description
};
因为您正在进行外连接,car
可以是null
-您应该在select
中防止它:
select new
{
FirstName = people.FirstName,
LastName = people.LastName,
Model = car == null ? null ; car.Model,
Desc = car == null ? null : car.Description
};
同样,你没有问,但你可以使用集合初始化器语法:
var peoples = new List<People>()
{
new People { Model = "Mustang", FirstName = "Joe", LastName = "L'indien" }),
new People { Model = "Corvet", FirstName = "Luc", LastName = "Emince" }),
new People { Model = "Verron", FirstName = "pierre", LastName = "Uneed" }),
...
};