score:0

that's because startswith returns a bool and you're saying select that bool based on whether it starts with that value or not. so actually, you're not even filtering on that value at all because you're not using a filter expression.

actually, you only need firstordefault as the list is already a list<string>

string[] allwebtemplatesettings = sitelidmaatschapsettings.current.provisioningsettings;
 var webtemplate = allwebtemplatesettings
    .firstordefault(x => x.startswith(string.format("template:{0}", web.webtemplate)));

score:1

you are confusing select, which selects a new value based on each existing value of a sequence, with where, which filters a sequence so it only contains items where a condition is met.

the simplest change is to replace your usage of select with where.

string[] allwebtemplatesettings = sitelidmaatschapsettings.current.provisioningsettings;
var webtemplate = allwebtemplatesettings
    .where(x => x.startswith(string.format("template:{0}", web.webtemplate)))
    .firstordefault();

the other answers have rolled this usage of where into firstordefault without explaining your underlying confusion.

score:5

well, you're getting an ienumerable of bools with your select, then you pick the first one if there are any. that's why you're getting a bool as your answer.

i think what you actually want is this:

string[] allwebtemplatesettings = sitelidmaatschapsettings.current.provisioningsettings;
var prefix = string.format("template:{0}", web.webtemplate);
var webtemplate = allwebtemplatesettings
    .firstordefault(x => x.startswith(prefix));

i've moved the string formatting operation out of the predicate since it is wasteful to recompute it for each element in your collection (especially if the collection is long).

score:9

use where instead of select:

var webtemplate = allwebtemplatesettings.where(x => x.startswith(string.format("template:{0}", web.webtemplate))).firstordefault();

Related Query

More Query from same tag