Reputation: 301
I created a class with Entity Framework like this:
public class Lehrling
{
[Key]
public int lehrling_id { get; set; }
[Required]
[StringLength(25)]
public string vorname { get; set; }
[Required]
[StringLength(25)]
public string nachname { get; set; }
}
In my c# backend, i created a object of this class: Lehrling lehrling = new Lehrling();
now, i want to assign a method to my object lehrling
like this:
lehrling = dataMgr.GetLehrling(lehrlingID, requestedMd5key);
lehrlingID
is type int
and requestedMd5key
is type string
The Method GetLehrling(lehrlingID, requestedMd5key)
was created like this in my Datamanager.cs
public List<Lehrling> GetLehrling(int lehrlingID, string md5key)
{
Uri weburi = new Uri(baseuri, API_LEHRLING + lehrlingID + '/' + md5key);
try
{
HttpResponseMessage response = httpClient.GetAsync(weburi).Result;
var json = response.Content.ReadAsStringAsync().Result;
JavaScriptSerializer ser = new JavaScriptSerializer();
List<Lehrling> data = ser.Deserialize<List<Lehrling>>(json);
//Für Sortierung Wichtig
return data;
}
catch (Exception)
{
throw;
}
}
as you can see it returns a List<Lehrling>
data
The Reason why i get a message is clear, the method is type
List<Lehrling>
and the variable lehrling
is type Lehrling
How can I fix this Error, any suggestions?
Upvotes: 1
Views: 50
Reputation: 2266
Well, as you said you actually need multiple items:
Lehrling lehrling = new Lehrling();
Needs to be
List<Lehrling> lehrlingList = new List<Lehrling>();
lehrlingList = dataMgr.GetLehrling(lehrlingID, requestedMd5key);
That should eliminate the error.
Upvotes: 1
Reputation: 5776
You should choose an item from collection.
lehrling = dataMgr.GetLehrling(lehrlingID, requestedMd5key).FirstOrDefault(l => some codition);
Or just take first one.
lehrling = dataMgr.GetLehrling(lehrlingID, requestedMd5key).FirstOrDefault();
Both variant may lead to NullPointerExeption
if no items were in collection.
Upvotes: 1