Reputation:
Seem to be having a problem trying to set a class to my linq variable:
Here's the class for manipulating data from the database:
public class Result
{
public bool LongerThan10Seconds { get; set; }
public int Id { get; set; }
public DateTime CompletionTime { get; set; }
public double longitude { get; set; }
public double latitude { get; set; }
}
Then I use this LINQ Statement in regards to getting data from the specific database:
foreach (var query in data.AssetStatusHistories
.Where(x => x.TimeStamp <= ft)
.Where(x => x.AssetID == guid))
{
if (lastResult != null)
{
if ((query.TimeStamp - lastResult.CompletionTime).TotalSeconds > 10)
{
dataResults.Add(new Result() { Id = query.ID,
longitude = query.Longitude,
latitude = query.Latitude });
}
lastResult = query; // <-- Error Is Here
}
}
The Error being lastResult = Query, I keep getting the Cannot Implicitly Convert "Project.Data" to "Project.Result" does anyone have any recommended solutions?
I've seen a few in regards to this error but not specifically to LINQ.
Upvotes: 1
Views: 129
Reputation: 3255
It tells you exactly what it means: you cannot convert Project.Data
to Project.Result
.
data.AssetStatusHistories.Where(x => x.TimeStamp <= ft)
.Where(x => x.AssetID == guid)
does not return a collection of Project.Result
objects, but objects of type Project.Data
.
EDIT
If your lastResult is of type Result
, I assume you could just type:
lastResult = new Result(){Id = query.ID,
longitude = query.Longitude,
latitude = query.Latitude });
But it won't work in that case. It's NOT a linq-related problem. Your lastResult
object is assigned somewhere else. If you want to assign a new lastResult
object at the end, where do you want to get it's CompletionTime
property value from? In code I've pasted is not assigning it anywhere.
Upvotes: 4
Reputation: 3463
**lastResult = query;** <--Error Is Here
the variable lastResult
will probably of type IQueryable<Project.Result>
, but you're putting in a collection of IQueryable<Project.Data>
from the data.AssetStatusHistories.Where(x => x.TimeStamp <= ft) .Where(x => x.AssetID == guid)
code.
Basically it means one cannot convert a Project.Data
to Project.Result
.
Try maybe giving us more context of the Project.Data class, so we can see what you're trying to achieve? And what is happening BEFORE the foreach loop. And why are you saying lastResult=query
?
By providing these details we might maybe help out more!
Upvotes: 0