左外连接提供错误消息'无法将类型'System.Nullable`1'强制转换为'System.Object'。'

时间:2014-03-12 15:49:09

标签: c# linq left-join

这让我疯了!

我尝试了很多种方法(以及每种方式的多次迭代),但得到了同样的错误。

我在LINQPad中尝试过查询并获得所需的结果。

场景:码头。我想要一张带有船只细节的所有单据的清单,如果一艘船被注册为停留在滑道上。如果在滑动处没有注册船只,则boatID字段可能为NULL(我知道这应该设置为密钥,但我尝试使用Linq来获得答案而不更改数据库)。有一个带有滑动清单的“滑动”表,包括一个BoatId区域(当船只在滑道上注册时)。第二张桌子是一张'Boat'牌桌,以BoatId作为钥匙和其他船只细节。

这是一个SQL查询(产生我想要的结果):

Select s.SlipID, s.SlipNumber, s.Length, s.Electricity, s.Telephone, s.TV,
b.BoatName+' ['+b.BoatType+', '+convert(varchar,b.BoatOverAllLength)+']' as boatDets, 
s.Status 
from Slip as s left outer join boat as b on s.BoatID = b.BoatId;

以下是给出错误的解决方案之一(但在LINQPad中有效):

var slipDets6 = from s6 in db.Slips
                join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
                from jn in temp.DefaultIfEmpty()
                orderby s6.SlipNumber 
                select new 
                { 
                    SlipID = (int?) s6.SlipId, 
                    SlipNumber = s6.SlipNumber, 
                    Length = s6.Length, 
                    Electricity = s6.Electricity, 
                    Telephone = s6.Telephone, 
                    TV = s6.TV, 
                    BoatDets = jn.BoatName + " [" + jn.BoatType + ", " + jn.BoatOverAllLength + "]", 
                    Status = s6.Status 
                };

我收到的实际错误代码是:

无法将类型System.Nullable'1强制转换为System.Object类型。 LINQ to Entities仅支持转换EDM原语或枚举类型。

我已经深入研究了我可以在这个网站(和其他网站)上找到的解决方案,但我似乎正在做正确的事情。

2 个答案:

答案 0 :(得分:0)

1- in .netframework4你不能在linq中使用enumerations到实体,但在.netframework4.5中你可以,在.netframework4中使用enumeration这个异常发生时,虽然我看不到您的代码中的任何enumeration ...

2-您可以逐个评论select中的属性,找到导致异常的属性,并检查其类型......

3-可能你没有任何问题,如果获取数据之前选择它们为literal这样:

var slipDets6 = (from s6 in db.Slips
                 join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
                 from jn in temp.DefaultIfEmpty()
                 orderby s6.SlipNumber
                 select new {s6, jn})
                 .ToList()
                 .Select(u => new
                     {
                         SlipID = (int?)u.s6.SlipId,
                         SlipNumber = u.s6.SlipNumber,
                         Length = u.s6.Length,
                         Electricity = u.s6.Electricity,
                         Telephone = u.s6.Telephone,
                         TV = u.s6.TV,
                         BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
                         Status = u.s6.Status
                     })
                 .ToList();

4-注意,如果jn为空,则此行BoatDets = u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",会导致异常

答案 1 :(得分:0)

根据您的评论,问题是您尝试从create a string BoatName, BoatType and BoatOverAllLength,你不能格式化linq to entities中的字符串,正如我之前所说(prev post,no3),你可以获取你需要的数据,然后在内存中select来创建BoatDets字符串,所以这肯定有效:

var slipDets6 = (from s6 in db.Slips
             join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
             from jn in temp.DefaultIfEmpty()
             orderby s6.SlipNumber
             select new {s6, jn})
             .ToList()
             .Select(u => new
                 {
                     SlipID = (int?)u.s6.SlipId,
                     SlipNumber = u.s6.SlipNumber,
                     Length = u.s6.Length,
                     Electricity = u.s6.Electricity,
                     Telephone = u.s6.Telephone,
                     TV = u.s6.TV,
                     BoatDets = u.jn == null ? "" : u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
                     Status = u.s6.Status
                 })
             .ToList();

或者,您可以将BoatName, BoatType and BoatOverAllLength作为属性获取,并在获取查询时,从该属性创建所需的字符串,如下所示:

public class FlatSlip
    {
        public int? SlipID { get; set; }
        public string SlipNumber { get; set; }
        public string Length { get; set; }
        public string Electricity { get; set; }
        public string Telephone { get; set; }
        public string TV { get; set; }
        public string BoatName { get; set; }
        public string BoatType { get; set; }
        public string BoatOverAllLength { get; set; }
        public string Status { get; set; }

        public string BoatDets
        {
            get
            {
                return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
            }
        }
    }

var slipDets6 = from s6 in db.Slips
                        join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
                        from jn in temp.DefaultIfEmpty()
                        orderby s6.SlipNumber
                        select new FlatSlip()
                        {
                            SlipID = (int?)s6.SlipId,
                            SlipNumber = s6.SlipNumber,
                            Length = s6.Length,
                            Electricity = s6.Electricity,
                            Telephone = s6.Telephone,
                            TV = s6.TV,
                            BoatName = jn == null ? "" : jn.BoatName,
                            BoatType = jn == null ? "" : jn.BoatType,
                            BoatOverAllLength = jn == null ? "" : jn.BoatOverAllLength,
                            Status = s6.Status
                        };

或者如果您坚持使用literal

public class Boat
    {
        public Boat()
        {
        }

        public Boat(string BoatName, string BoatType, string BoatOverAllLength)
        {
            this.BoatName = BoatName;
            this.BoatType = BoatType;
            this.BoatOverAllLength = BoatOverAllLength;
        }

        public string BoatName { get; set; }
        public string BoatType { get; set; }
        public string BoatOverAllLength { get; set; }

        public string BoatDets
        {
            get
            {
                return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
            }
        }
    }

        var slipDets6 = (from s6 in db.Slips
                         join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
                         from jn in temp.DefaultIfEmpty()
                         orderby s6.SlipNumber
                         select new { s6, jn })
                         .ToList()
                         .Select(u => new
                         {
                             SlipID = (int?)u.s6.SlipId,
                             SlipNumber = u.s6.SlipNumber,
                             Length = u.s6.Length,
                             Electricity = u.s6.Electricity,
                             Telephone = u.s6.Telephone,
                             TV = u.s6.TV,
                             BoatDets = jn == null ? new Boat() : new Boat(u.jn.BoatName, u.jn.BoatType, u.jn.BoatOverAllLength),
                             Status = u.s6.Status
                         })
                         .ToList();

注意:BoatDets属性,在我的最后两个查询中,不能在linq to entity中使用,并且当您的数据被提取到内存时它是可读的