Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core ef-core-3.1

Join returns duplicate values

发布于 2020-03-31 22:56:05

I was porting an existing .NET code to .NET core.

There is a specific join code that is not working in EFCore. The code is :

var values = (from config in tblConfig
                               join info in tblInfo on config.No equals info.No
                               where (
                                     //some conditions
                                     )
                               select config).ToList();

In EFCore this returns a collection where most of the data are duplicated. Especially, there is a column regNo which in some cases are "-1". This value is duplicated in all the rows after the join.

I tried the grouping, removing the conditions etc as mentioned in some other post. But didn't work. I am pointing this column since it is easy to understand.

Config Table enter image description here

Info Table enter image description here

Based on the No field I need to join. It seems all the empty cells replaced with non-empty content from same table.

Questioner
ACB
Viewed
18
ACB 2020-02-03 13:06

In my case I was getting duplicate values ( First row is repeated for all the other rows).

This occurs when we retrieve values from a table having no unique keys. The number of rows returned will be correct but they will be duplicates of the first row. To solve, use AsNoTracking . I got the clue from forums.asp.net

When this is used chances are there that it will miss the Navigation properties. See this answer to fix that.