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();
Source: stackoverflow.com
Related Query
- why is this linq query return a boolean and not the first result of the select?
- Why does my LINQ to SQL query fail the first time ("Row Not Found or Changed") and succeed the second time?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Why is this LINQ query not returning the correct dates?
- Getting the first result from a LINQ query - why does ElementAt<T>(0) fails when First<T>() succeeds?
- Why does this linq query fail when I change the select clause to select new Model
- Why is the Where clause in this LINQ Query not appending?
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- Converting linq select into model, LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- Why does this LINQ statement return null and not a IEnumerable with count=0
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- Take the first five elements and the last five elements from an array by one query using LINQ
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- Why does not null in LINQ query still return null records?
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- Why does a GC after a LINQ query free the WhereListIterator but not the Func representing the condition?
- Why is this LINQ query not executed when using foreach?
- why .ToList().Distinct() throws error but not the .Distinct().ToList() with linq query
- Result of LINQ Query in MVC SelectList as Value and Text - not working
- Why one query works and not the other?
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- Linq is this string.Compare in the query inefficient and is there a better way?
- Why not reveal the type and identity of the source to the client?
- why the sql query is different on that linq query when run on c# and on vb.net?
More Query from same tag
- DB Context - Query to select post from a category
- How to read a sitemap using VB.NET
- How do I add a percentage to a value in LINQ?
- Custom Generic.IEqualityComparer(Of T) - Compiler Errors
- C# Entity Framework - Sort View when reading from it
- Converting sql query to EF query- nested query in from
- Using select distinct on DataTable
- Replace specific Objects in IQueryable query in .NET fails
- Specified cast is invalid when using firstordefault
- Finding duplicate values in row(s) in different tables using linq vb.net
- Linq - How to convert from flat to hierarchical?
- Comparing Datetime? inside Linq Expression
- How to use Linq objects to validate a view in MVC 2
- C# equivalent of C++ std::string find
- Unable to pass multiple values in the where clause of a LINQ query
- WHERE IN CLAUSE for LINQ to XML
- Records issue using top keyword in sql query
- Copy a List<SomeClass> into other but applying a function to a property in LINQ
- EF Code First- Return Records from Many to Many Table
- Updating Telerik Rad Grid?
- Getting the Total Number of Elements From All Lists Belonging to An Object
- Cannot convert IQueryable<IEnumerable<string>> to return type IEnumerable<string>
- Making an outer join on two anonymous type lists using LINQ in Entity Framework
- How to convert this T-SQL query to LINQ syntax?
- Datatable C# - How to get detail record from one table and its associated sub record from another table based on ID
- Reuse stored procedure result in linq to sql in Code First
- Is there a way to extract primitive fields from a Dictionary of objects in C#?
- Use reflection and lambda expression with linq
- Is there a standard LINQ operator that will transform an IEnumerable<T> into another by looking ahead at its own elements
- What's elegant way to rewrite following LINQ statement using extension methods?