Linq result multiplies results

I have a simple SQL query:

SELECT table1.[idGK]  , table2.FullName , table2.LgotName
 from table2
 join table1 on table2.C_LGT = table1.[idGK] 
where table1.mcod = 41003 

And I have the correct conclusion:

idGK | FullName| LgotName 
------------------------                           
1    |One      |Ball                             
2    |Two      |Wog                           
3    |Three    |Aks  
5    |Four     |Mqi                             
7    |Five     |Thel                           
9    |Six      |Imx  

But when I make a LINQ query of this:

IEnumerable<FinalDoc> fidn = from post in repository.table1
join thir in repository.table2 on post.idGK equals thir.C_LGT
where  post.mcod.Trim().Contains("41003")

orderby post.idGK

select new FinalDoc
  {
     mcod = post.mcod,
    FullName= thir.FullName,
     idGK = post.idGK
 }; 

I have this result:

    FullName  | LgotName 
    ------------------------                           
    Five      |Thel                             
    Five      |Thel                            
    Five      |Thel  
    Five      |Thel                              
    Five      |Thel                            
    Five      |Thel 

I am trying to modify table1 and table 2 to make the correct connection, but I have the same result.
What linq query do i need to make the same result as in SQl?
PS EF, Linq, Asp.net, web forms

+4
source share
2 answers

, :
, , - .
, id, EF , , EF ( ). idG:

[Key]
    public int idG {get;set;}
    public int idGK { get; set; }
    public string mcod { get; set; }
0

LINQ SQL-:

from thir in repository.table2
join post in repository.table1 on thir.C_LGT equals post.[idGK]
where post.mcod == 41003

, SQL (.. table1.mcod - , ), .

EDIT: , , SQL EF LINQ. .

var query = from post in repository.table1
join thir in repository.table2 on post.idGK equals thir.C_LGT
where post.mcod.Trim().Contains("41003")

var sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();
+2

Source: https://habr.com/ru/post/1664682/


All Articles