Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I try to use this code however it show me the item more than one

how can i fix this?
C#
var element = (from h in ent.REPORTHEADERs
                           join d in ent.REPORTDETAILS on h.FIS_NO equals d.FIS_NO
                           select new
                           {
                               h.SUBE_ID,
                               h.FIS_NO,
                               h.TERAZI_NO,
                               h.TERAZI_REYON_NO,
                               d.URUN_NO,
                               d.BIRIM_FIYAT,
                               h.SATICI_NO,
                               h.MUSTERI_NO,
                               h.FIS_ADET,
                               h.FIS_AGIRLIK,
                               h.TUTAR,
                               h.IPTAL,
                               h.ILK_ISLEM_TARIHI,
                               h.SON_ISLEM_TARIHI,
                               h.FIS_SAYISI,
                               h.TERAZI_ADIIP
                           }).ToList();


What I have tried:

I tried to comment the join part and its work perfect however i add the join part i get same item 3 times
Posted
Updated 2-Jul-17 21:54pm
Comments
Ehsan Sajjad 3-Jul-17 4:07am    
it is because you probably have one to many relationship with details table from main table.
Member 10525430 3-Jul-17 4:11am    
how can i fix?

1 solution

You could try:
C#
.ToList().FirstOrDefault();

at the end of the ToList() method in order to get the first element that satisfies the statement if youre looking for a single value to come back.
 
Share this answer
 
Comments
Member 10525430 3-Jul-17 4:03am    
but its the datasource of gridview and when i erite firstordefault i get only one item but i have 3 item.
Prifti Constantine 3-Jul-17 4:17am    
So the problem is not that it gives more than one item, but that it shows one item more than once?
Member 10525430 3-Jul-17 4:19am    
actually,

my db have 3 item

however when i use this entity code i get 9 item on gridview
Prifti Constantine 3-Jul-17 4:28am    
You could iterate the List with a ForEach loop and for reach item that you want to get, you could use the .Distinct(); at the end so it will select A single value per item since you have 3 per item right now.
A way to implement this is :
ForEach var item in ent.REPORTHEADERs
{
var element = (from h in ent.REPORTHEADERs
join d in ent.REPORTDETAILS on h.FIS_NO equals d.FIS_NO
select new
{
h.SUBE_ID,
h.FIS_NO,
h.TERAZI_NO,
h.TERAZI_REYON_NO,
d.URUN_NO,
d.BIRIM_FIYAT,
h.SATICI_NO,
h.MUSTERI_NO,
h.FIS_ADET,
h.FIS_AGIRLIK,
h.TUTAR,
h.IPTAL,
h.ILK_ISLEM_TARIHI,
h.SON_ISLEM_TARIHI,
h.FIS_SAYISI,
h.TERAZI_ADIIP
}).Distinct.ToList();
}
Member 10525430 3-Jul-17 4:36am    
its not working correctly
when i use your code i get only 2 same item
the other 2 different item not come

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900