LINQ join问题,返回所有null值

时间:2013-01-19 14:26:13

标签: linq entity-framework c#-4.0

我是EFLINQ的新人,我面临着一个奇怪的问题。当我在select new块中检查空值时,子表中的所有值都为空。以下是LINQ查询。

我的Linq代码

var linqResult = from pd in entities.tblpackagedetails
         join ps in entities.tblpackageselecteds
         on pd.PackageDetailsID equals ps.PackageDetailsID 
         into tabJoin
         from tj in tabJoin.Where(ps => ps.UserID == userID 
         && ps.IsActive == true).DefaultIfEmpty()
         select new
         {
             IsComplete = (tj == null) ? false : tj.IsComplete,
             IsActive = (tj == null) ? false : tj.IsActive,
             UserID = (tj == null) ? 0 : tj.UserID,
             IsMandatory = pd.IsMandatory,
             PackageSelectedID = (tj == null) ? 0 : tj.PackageSelectedID,
             IsSelected = (tj == null ? false : tj.IsSelected),
             pd.Amount,
             pd.Code,
             pd.Description,
             pd.Points,
             pd.PackageDetailsID
         };

        foreach (var result in linqResult)
        {
            packagesSelected.Add(new PackageDetailDataModel()
            {
                Amount = result.Amount,
                Code = result.Code,
                Description = result.Description,
                IsComplete = result.IsComplete,
                IsMandatory = result.IsMandatory,
                PackageDetailsID = result.PackageDetailsID,
                PackageSelectedID = result.PackageSelectedID,
                Points = result.Points,
                IsActive = result.IsActive,
                UserID = result.UserID,
                IsSelected = result.IsSelected
            });
        }

Visualizer生成的SQL

    SELECT
    `Extent1`.`PackageDetailsID`, 
    `Extent2`.`IsComplete`, 
    `Extent2`.`IsActive`, 
    `Extent2`.`UserID`, 
    `Extent1`.`IsMandatory`, 
    `Extent2`.`PackageSelectedID`, 
    `Extent2`.`IsSelected`, 
    `Extent1`.`Amount`, 
    `Extent1`.`Code`, 
    `Extent1`.`Description`, 
    `Extent1`.`Points`
    FROM `tblpackagedetails` AS `Extent1` 
    LEFT OUTER JOIN `tblpackageselected` AS `Extent2` 
    ON (`Extent1`.`PackageDetailsID` = `Extent2`.`PackageDetailsID`) 
    AND ((`Extent2`.`UserID` = @linq_0) AND (1 = `Extent2`.`IsActive`))

当我在MySQL工作台上运行sql时,我获得了以下输出(使用userID重新生成@linq_0)。

workbench output

我的父表结构 tblPackageDetails

子表结构 tblPackageSelected

我想要的输出 Output wanted

IsCompleteIsActiveUserIDPackageSelectedIDIsSelected的值为select new块分配中的结果条件检查假或0。

如果我删除空检查,我得到前3行的值,在第四次迭代中我得到异常。 The cast to value type 'Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

请帮助...... :(

工作代码块

        packagesSelected = new List<PackageDetailDataModel>();
    var linqResult = from pd in entities.tblpackagedetails
                     join ps in entities.tblpackageselecteds
                     on pd.PackageDetailsID equals ps.PackageDetailsID
                     into tabJoin
                     from tj in tabJoin.Where(ps => ps.UserID == userID
                     && ps.IsActive == true).DefaultIfEmpty()
                     select new
                     {
                         IsComplete = (bool?)tj.IsComplete,
                         IsActive = (bool?)tj.IsActive,
                         UserID = (int?)tj.UserID,
                         IsMandatory = pd.IsMandatory,
                         PackageSelectedID = (int?)tj.PackageSelectedID,
                         IsSelected = (bool?)tj.IsSelected,
                         pd.Amount,
                         pd.Code,
                         pd.Description,
                         pd.Points,
                         pd.PackageDetailsID
                     };

    foreach (var result in linqResult)
    {
        packagesSelected.Add(new PackageDetailDataModel()
        {
            Amount = result.Amount,
            Code = result.Code,
            Description = result.Description,
            IsComplete = (result.IsComplete ?? false),
            IsMandatory = result.IsMandatory,
            PackageDetailsID = result.PackageDetailsID,
            PackageSelectedID = (result.PackageSelectedID ?? 0),
            Points = result.Points,
            IsActive = (result.IsActive ?? false),
            UserID = (result.UserID ?? 0),
            IsSelected = (result.IsSelected ?? false)
        });
    }

感谢2Kay:)

1 个答案:

答案 0 :(得分:2)

tjnull时,EF会将tj的所有属性视为null。没关系,但是当EF试图将它们实现为值类型时,它就失败了。因此解决方案是使用可空类型..

尝试此查询:

var linqResult = from pd in entities.tblpackagedetails
     join ps in entities.tblpackageselecteds
     on pd.PackageDetailsID equals ps.PackageDetailsID 
     into tabJoin
     from tj in tabJoin.Where(ps => ps.UserID == userID 
     && ps.IsActive == true).DefaultIfEmpty()
     select new
     {
         IsComplete = (bool?) tj.IsComplete,
         IsActive = (bool?) tj.IsActive,
         UserID = (int?) tj.UserID,
         IsMandatory = pd.IsMandatory,
         PackageSelectedID = (int?) tj.PackageSelectedID,
         IsSelected = (bool?) tj.IsSelected,
         pd.Amount,
         pd.Code,
         pd.Description,
         pd.Points,
         pd.PackageDetailsID
     };