开发者

How to include related Entities using dynamic queries

开发者 https://www.devze.com 2022-12-27 06:15 出处:网络
I am taking the values from a search form in my application to build a dynamic query: string queryString = @\"SELECT VALUE USERS FROM ProjectDBEntities.Users AS Users

I am taking the values from a search form in my application to build a dynamic query:

string queryString = @"SELECT VALUE USERS FROM ProjectDBEntities.Users AS Users 
WHERE ";

There are two tables in the database, Users and Photo, table Photo has a column UserId that links t开发者_如何学编程o the Users table. A one to many relationship exists between Users and Photo.

After some iteration through the form values and adding System.Data.Objects.ObjectParameter values, I end up with following query:

SELECT VALUE USERS FROM ProjectDBEntities.Users AS Users 
WHERE Users.CountryId = 2

Then I have this code:

System.Data.Objects.ObjectQuery<Users> usersQuery =
                new System.Data.Objects.ObjectQuery<Users>(queryString, _db);

The usersQuery object does not contain the Image data for each Users. In my View I can iterate through the Users.Image but the Image count is always zero. Do I have to include or attach the Image data somewhere? How?


Just add an .Include() for the image property:

System.Data.Objects.ObjectQuery<Users> usersQuery =
            new System.Data.Objects.ObjectQuery<Users>(queryString, _db).Include("Image");
0

精彩评论

暂无评论...
验证码 换一张
取 消