Reputation: 1171
I've got linq which joins two lists by id and returns strongly typed List:
List<TestowaKlasaDlaLinq> linqlambda = ListDetailedData.Join(PlacesList, a => a.place.id, b => b.id, (a, b) =>
new TestowaKlasaDlaLinq()
{
Czas = a.startDate.TimeOfDay.ToString(),
Co = a.name,
Miasto = b.address.city,
Miejsce = a.organizer.designation
}).ToList();
For now everything works. Problem appeared when I tried to filter that list e.g.:
var onlyGdansk = linqlambda.Where(x => x.Miasto.Equals("Gdańsk")).Select(x => x).ToList();
How is this possible that strongly field of strongly typed List of type "TestowaKlasaDlaLinq" doesnt exist in that context?
Upvotes: 3
Views: 542
Reputation: 30492
A variant of Gauran Dave's answer: do not put the Equals function on the string in property Miasto
, because that string could be null, but put the Equals function on the string "Gdańsk". You may be assert that "Gdańsk" is not null.
var onlyGdansk = linqlambda.Where(x => "Gdańsk".Equals(x)))
.Select(x => x) // this one can be omitted
.ToList();
Or consider using String.Equals(string, string), which IMHO is a bit more readable here:
var onlyGdansk = linqlambda.Where(x => String.Equals(x, "Gdańsk"))
.Select(x => x)
.ToList()
Upvotes: 0
Reputation: 4046
I think, One of the x.Miasto value is null and that is the reason, it shows you this error.
var onlyGdansk = linqlambda.Where(tkdl => tkdl.Miasto!=null && tkdl.Miasto.Equals("Gdańsk")).Select(dl => dl).ToList();
Upvotes: 3