Join in Entity Framework返回意外值

时间:2016-08-14 11:27:06

标签: c# entity-framework join

我有这个查询

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条记录,此表与使用material的{​​{1}}有关系。在我的Lines表中,我有20条记录,输出重复20次,值相同。

为什么呢?我该如何解决?

这是主要查询

material

1 个答案:

答案 0 :(得分:1)

您可以使用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();