March 27, 2019 09:46 by
Peter
So I was plugging right along in Silverlight using LINQ to asynchronously pull data from our database into my C# code. Everything was going great until I attempted to pull data from one table and its related tables all in one query. Here is what I found which resolved my data problem.
In my Library class I have the following code that enables my ASP.NET code to query a User by UserID and return a User object along with their UserFavorites and Illustration objects. This gives me everything I need to know about the user and their favorite illustrations.
public IQueryable<MyLibrary.User> GetUserByID(int userID)
{
return myContext.Users.Include("UserFavorites").Include("UserFavorites.Illustration")
.Where(u => u.UserID == userID);
}
In Silverlight I had a need to perform the same query using LINQ. After much searching on the web I found the two things that were needed to make this happen.
1. Use "Expand" instead of "Include"
2. Instead of "UserFavorites.Illustration" replace the "." with a "/" to get "UserFavorites/Illustration".
int userID = 0;
var qUser = ((DataServiceQuery<User>)(from myUser in service.Users
where myUser.UserID.Equals(userID)
select myUser))
.Expand("UserFavorites")
.Expand("UserFavorites/Illustration");
Now I have all of my data and I am happy once again. Using the Expand on my query is very nice in that I can get all of my data in one asynchronous call.
HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.