I’m new with lambda expressions and linq and can’t figure out what I’m doing wrong here:
GroupSet groupToChange = context.GroupSet.Select(q => q.groupId == groupId);
I’m trying to get and change the name of an entity.
groupToChange.groupName = newGroupName;
I’m not having any problems with the second line though. Any ideas? It tells me I can’t convert bool to GroupSet but the function returns what it finds, right?
You want to use
Whereinstead ofSelect.Wherereturns everything that matches the supplied lambda expression.Selectis a transformation. It transforms each element using the supplied lambda expression.Additionally,
Wherereturns anIEnumerable<T>, because it can’t know that only one element will match your condition. If you know it will only return one result, appendSingleto the query, to return only one element. Only then it will compile:If you are unsure, whether or not one element will match, use
SingleOrDefault:The difference between those two:
Singlewill throw an exception, if the result set ofWhereis empty.SingleOrDefaultwill returndefault(T), i.e.nullin your case.