Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a problem with linq join query.
I have admin table with foreign key of city table.

When i write following linq query it throw error at databind time.
C#
MyEntityDataContext MyEntity = new MyEntityDataContext();
 return (from a in MyEntity.tblAdmins
         join c in MyEntity.tblCities
         on a.city equals c.cityid
         select new { a, c.cityname }).ToList();


I get this error
=====================
DataBinding: '<>f__AnonymousType0`2[[tblAdmin, App_Code.xexoenga, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'id'.


table admin
=================
id	int	not null pk
firstname	varchar(50)	null
lastname	varchar(50)	null
username	varchar(50)	null
password	varchar(50)	null
emailid 	varchar(50)	null
hobbies 	varchar(50)	null
gender  	bit	        null
city           	int	null
isactive	bit	null


tblCities
=========================================
cityid	int	not null pk
cityname	varchar(50)	null



I am getting correct answer when write following query, but in some table i have over 40 column,
so i want to get query like select a.*,b.cityname etc in linq

C#
return (from a in MyEntity.tblAdmins
                join c in MyEntity.tblCities
                on a.city equals c.cityid
                select new { a.firstname,a.lastname, c.cityname }).ToList();
Posted
Updated 9-Sep-12 21:04pm
v4
Comments
sanwar_mal_jat 10-Sep-12 2:27am    
tell me what u bind in your databind control
CodeHawkz 10-Sep-12 2:30am    
It is nearly impossible for people to answer you without seeing the table or the class structure of the 'tblAdmins' and 'tblCities', don't you think? Most probably, you would have got an answer by now, if you did :)
Menon Santosh 10-Sep-12 2:34am    
post your table structure
Mohamed Mitwalli 10-Sep-12 3:07am    
Hi ,
Make sure you update your DBML file

Here you go with this query:
C#
var query = from TableA in MyEntity.tblAdmins
            join TableB in MyEntity.tblCities on TableA.city 
            equals TableB.cityid into tblTemp
            from sub in tblTemp.DefaultIfEmpty()
            select new { MyRow = TableA, sub.cityname  };



All the best.
--Amit
 
Share this answer
 
v2
Every thing is ok with this but select id also then error will be gone



MyEntityDataContext MyEntity = new MyEntityDataContext();
return (from a in MyEntity.tblAdmins
join c in MyEntity.tblCities
on a.city equals c.cityid
select new { a.id, c.cityname }).ToList()
 
Share this answer
 

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