Silverlight using Entity to LINQ on Joins
I have the following code 开发者_开发百科which works and gets me the data for a single entity.
LoadOperation<TimeForm> loadoperation = _cc.Load(_cc.GetTimeFormsQuery()
.Where(o => o.Start>= weekrange.startdate
&& o.End<= weekrange.enddate
&& o.USERID== "TEST"));
However I have 3 tables which connect to this TimeForm table, in sql my query looks like this:
SELECT T* FROM TimeForm
INNER JOIN CLIENT ON TimeForm.CODEID= CLIENT.CODEID
INNER JOIN RATE ON TimeForm.RATEID= RATE.RATEID
INNER JOIN TASK ON TimeForm.TASKID = TASK.TASKID
How can this be possible with the above syntax? I need some values from these tables.
Try something like this:
var query = context.TimeForm.
Join(context.CLIENT,
t => t.CODEID, c => c.CODEID ,
(t, c) => new
{
PropertyA = t.ColumnA,
PropertyB = c.ColumnB
}).Join(context.RATE,
b => b.RATEID, r => r.RATEID,
(b, r) => new
{
PropertyC = b.ColumnC,
PropertyD = r.ColumnD
}).Join(context.TASK,
x => x.TASKID, t => t.TASKID,
(x,t) => new
{
PropertyE = x.ColumnE,
PropertyF = t.ColumnF
});
PropertyA, B, etc are just properties present in the type, which you use to store the data returned from the query. Whereas ColumnA, B, etc are columns present in the tables involved in the join. You can substitute actual values for these in your query.
You need to go to the Domain Services file (where the GetTimeFormsQuery() is defined). It'll look something like:
public IQueryable<TimeForm> GetTimeForms() {
return this.Context.TimeForm;
}
, and add to it so it is like this:
public IQueryable<TimeForm> GetTimeForms() {
return this.Context.TimeForm
.Include("Client") // Assuming your property to see the client is called "Client"
.Include("Rate") // Same for "Rate"
.Include("Task"); // and "Task
}
Or whatever the navigation properties are called in your TimeFrom entity.
Silverlight doesn't do lazy loading, so you'll have to explicitly include these properties in the query in the domain service. Also it's probably wise to create an extra method on the domain service that accepts the start and end date and userid so that you don't pull the entire table over the wire every time.
public IQueryable<TimeForm> GetTimeFormsWithStartAndEnd(DateTime start, DateTime end, string userId) {
return this.Context.TimeForm
.Include("Client") // Assuming your property to see the client is called "Client"
.Include("Rate") // Same for "Rate"
.Include("Task") // and "Task
.Where(o => o.Start>= start
&& o.End<= end
&& o.USERID== userId));
}
After you rebuild your web-project, you'll have a method called GetTimeFormsWithStartAndEndQuery in your silverlight with these 3 as parameters.
Goodluck!
精彩评论