Reputation: 6890
I have the following code:
ServiceSoapClient service = new ServiceSoapClient();
var dataSource = (from u in _db.Sessions
where u.DeletedOn == null
select new
{
SessionId = u.UploadSessionId,
FileName = u.FileName,
CreatedBy = u.User.UserName,
Partner = u.Company.Name,
CreatedOn = u.CreatedOn,
Visible = service.IsSessionProcessing(u.UploadSessionId)
})
.OrderByDescending(x => x.CreatedOn);
...of course the problem here is the Visible = service.IsSessionProcessing(u.UploadSessionId)
portion that has access to modified closure because the expression is actually calculated when I use dataSource
somewhere i.e preform .ToList()
or something similar.
The problem here is that I can't perform .ToList()
immediately because I need to use it as a data source for a control as it is.
Is there any way to avoid this in Linq? Can I use a local variable in the expression itself so that it gets calculated with the real values?
Upvotes: 1
Views: 719
Reputation: 7671
The real bug is in trying to call function in context of expression, which make no sence for an expression representing a database operation. I suppose you actually tried the following:
// Database operation, no soap client funny business here.
var list = (
from u in _db.Sessions
where u.DeletedOn == null
orderby x.CreatedOn descending
select new
{
SessionId = u.UploadSessionId,
FileName = u.FileName,
CreatedBy = u.User.UserName,
Partner = u.Company.Name,
CreatedOn = u.CreatedOn,
}).ToList();
// From now on you are not in a database anymore, it's pure linq-to-objects. You can call the service.
ServiceSoapClient service = new ServiceSoapClient();
var dataSource = (from u in list select new {
SessionId = u.SessionId,
FileName = u.FileName,
CreatedBy = u.CreatedBy,
Partner = u.Partner,
CreatedOn = u.CreatedOn,
Visible = service.IsSessionProcessing(u.SessionId)
}).ToList();
Upvotes: 0
Reputation: 1062512
The "modified closure" here is service
. So... don't reassign service
.
Upvotes: 1