加入实体框架返回意外值

本文关键字:意外 返回 框架 实体 | 更新日期: 2023-09-27 18:02:17

我有这个查询

var q = from internalMaterialIssueVocherDetail in _ctx.InternalMaterialIssueVoucherDetails
        where internalMaterialIssueVocherDetail.InternalMaterialIssueVoucherId == Id
        join line in _ctx.Lines on internalMaterialIssueVocherDetail.LineId equals line.Id 
        join material in _ctx.Materials on line.Id equals material.LineId

我在InternalMaterialIssueVoucherDetails中有1条记录,这个表使用Linesmaterial有关系。在我的material表中,我有20条记录,输出重复20次,具有相同的值。

为什么?怎么解呢?

下面是主查询

var q = from internalMaterialIssueVocherDetail in _ctx.InternalMaterialIssueVoucherDetails
        where internalMaterialIssueVocherDetail.InternalMaterialIssueVoucherId == Id
        join line in _ctx.Lines on internalMaterialIssueVocherDetail.LineId equals line.Id
        join joint in _ctx.Joints on internalMaterialIssueVocherDetail.JointId equals joint.Id
        join sheet in _ctx.Sheets on joint.SheetId equals sheet.Id
        join material in _ctx.Materials on line.Id equals material.LineId 
        //join materialDescription in _ctx.MaterialDescriptions on material.MaterialDescriptionId equals materialDescription.Id
        join testPackageJoint in _ctx.TestPackageJoints on joint.Id equals testPackageJoint.JointId
        join testPackage in _ctx.TestPackages on testPackageJoint.TestPackageId equals testPackage.Id
        select new ViewIMIV()
                   {
                      // ItemCode = materialDescription.ItemCode,
                      // MaterialDescription = materialDescription.Description,
                      SheetNumber = sheet.SheetNumber,
                      LineNumber = line.LineNumber,
                      TestPackageNumber = testPackage.PackageNumber,
                      QuantityDeliverToMember = internalMaterialIssueVocherDetail.QuantityDeliverToMember.ToString(),
                      //Size = materialDescription.Size1
                   };

加入实体框架返回意外值

您可以使用Distinct删除重复项:

(from ........
select new
{
    ItemCode = materialDescription.ItemCode,
    MaterialDescription = materialDescription.Description,
    SheetNumber = sheet.SheetNumber,
    LineNumber = line.LineNumber,
    TestPackageNumber = testPackage.PackageNumber,
    QuantityDeliverToMember = internalMaterialIssueVocherDetail.QuantityDeliverToMember.ToString(),
    Size = materialDescription.Size1
}).Distinct().Select(x => new ViewIMIV()
{
    ItemCode = x.ItemCode,
    MaterialDescription = x.MaterialDescription,
    SheetNumber = x.SheetNumber,
    LineNumber = x.LineNumber,
    TestPackageNumber = x.TestPackageNumber,
    QuantityDeliverToMember = x.QuantityDeliverToMember,
    Size = x.Size
}).ToList();