Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi team,

I have the two table tempUserCityHotelAccess and UserCityHotelAccess.
I have to insert data record from tempUserCityHotelAccess to UserCityHotelAccess through Entity framework.

my code is

C#
int sessionUsrId = SessionVariables.intUSerID;
tempUserCityHotelAccess item = new tempUserCityHotelAccess();
tempUserCityHotelAccess temptable = (from t in db.tempUserCityHotelAccesses where t.IntUserId ==  (sessionUsrId)  select t);

UserCityHotelAccess maintable  = new UserCityHotelAccess();
maintable.IntUserId = sessionUsrId;
maintable.intHotelId = temptable.intHotelId;
maintable.intCityId = temptable.intCityId;
db.UserCityHotelAccesses.Add(maintable);
db.SaveChanges();


In Where condition i am getting error for casting.
Please any body help me for the same and give me the code for the inserting record through Entity framework from one table to another table.

Thanks in advanced.
Posted
Updated 22-Oct-14 8:46am
v2

1 solution

My best guess without you telling us exactly what the error is, is that your query does not return a tempUserCityHotelAccess but an IQueriable or other similar data type. This is because you query could potentially return more than one result.

If you can guarantee that only a single object will be returned you can use .Single(), else you could use .First() of perhaps .FirstOrDefault()

Here is a link to a stack overflow question on when to use Find, First, Single etc. Entity Framework 4 Single vs First vs FirstOrDefault[^]

int sessionUsrId = SessionVariables.intUSerID;
tempUserCityHotelAccess item = new tempUserCityHotelAccess();
tempUserCityHotelAccess temptable = (from t in db.tempUserCityHotelAccesses where t.IntUserId == (sessionUsrId) select t).Single();
 
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