My select statement does not return the entire Customer object, just the utrustningNr and beskrivning. How do i return it in a correct way without getting this error:
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<fjallvick.utrustning>
public List<utrustning> searchEquipment(string equipmentNr)
{
var queryEquipment = from utrustning in globalDBConnString.context.utrustnings
where utrustning.utrustningNr == equipmentNr
select (new { utrustning.utrustningNr, utrustning.beskrivning });
return queryEquipment.ToList();
}
The problem is that your select
clause is creating a new anonymous type for each item, instead of a new utrustning
. You may well want something like:
var queryEquipment = from utrustning in globalDBConnString.context.utrustnings
where utrustning.utrustningNr == equipmentNr
select new utrustning {
utrustningNr = utrustning.utrustningNr,
beskrivning = utrustning.beskrivning
};
Alternatively, if globalDBConnString.context.utrustnings
actually returns a sequence of ustrustning
values already, you could just use:
return globalDBConnString.context.utrustnings
.Where(u => u.utrustningNr == equipmentNr)
.ToList();
If you want to only return two properties, you may well be best off creating a new type which only has those two properties - then change the return type of the method, and change the select
clause to use that.
Side-note: your code would be easier to understand if you followed normal .NET naming conventions.
See more on this question at Stackoverflow