外部联接中的内部异常

Posted

技术标签:

【中文标题】外部联接中的内部异常【英文标题】:Inner Exception in outer Join 【发布时间】:2011-12-22 10:23:45 【问题描述】:

我不知道为什么我的程序不起作用。请问你能帮帮我吗 !当我运行它时,我有一个内部异常。

  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));
   

【问题讨论】:

欢迎来到 ***。你能给我们例外文本吗?是哪个例外? 不是文本 - 完全例外。 【参考方案1】:

在您的选择中定义字段名称:

               select new
               
                  FirstName = people.FirstName,
                  LastName = people.LastName,
                  Model = car.Model,
                  Desc = (people == null) ? "CASSE DE MERDE" : car.Description
                ;

【讨论】:

【参考方案2】:

因为您正在执行外部连接,所以 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" ),
    ...
;

【讨论】:

people 的空保护要么不完整,要么毫无意义。

以上是关于外部联接中的内部异常的主要内容,如果未能解决你的问题,请参考以下文章

在多列上组合 sql 内部和外部联接

详解T-SQL的联接机制

ORACLE表与表联接的几种方式

外部连接的位置vs ON

SQL Server-外部联接基础回顾(十三)

Clojure 中的外部联接