score:3
AllIDs
is a list of IAddress
but you are selecting a string
. The compiler is complaining it cannot convert a List<string>
to a List<IAddress>
. Did you mean the following instead?
var substrings = AllIDs.Where(...).Select(...).ToList();
score:0
If you want to put them back into Address
objects (assuming you have an Address
class in addition to your IAddress
interface), you can do something like this (assuming the constructor for Address
is in place):
AllIDs = AllIDs.Where(...).Select(new Address(s.AddressID.Substring(s.AddressID.IndexOf("_")))).ToList();
You should also look at using query syntax for LINQ instead of method syntax, it can clean up and improve the readability of a lot of queries like this. Your original (unmodified) query is roughly equivalent to this:
var substrings = from a in AllIDs
let id = a.AddressId
let idx = id.IndexOf("_")
where id.Length >= idx
select id.Substring(idx);
Though this is really just a style thing, and this compiles to the same thing as the original. One slight difference is that you only have to call String.IndexOf()
one per entry, instead of twice per entry. let
is your friend.
score:0
Maybe this?
var boundable =
from s id in AllIDs
where s.AddressId.Length >= s.AddressId.IndexOf("_")
select new { AddressId = s.AddressId.Substring(s.AddressId.IndexOf("_")) };
boundable = boundable.ToList();
Source: stackoverflow.com
Related Articles
- What is wrong in this LINQ Query, getting compile error
- What is wrong with this LINQ query
- Why am I getting a Compile time error in this linq query?
- What is wrong with this linq query?
- What does this LINQ query do?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- What Is This Part Of A Linq Query Called?
- Getting Error "Delegate 'System.Func<EntityType,int,bool> does not take 1 arguments" from Entity Framework LINQ query
- What would be a reasonably fast way to code this sql query in c#?
- LINQ: Why am I getting error on this LINQ Query?
- LINQ with Where and All not filtering data - What is wrong with this query?
- I am using linq and I am getting the error "There is already an open datareader associated with this command which must be closed first"
- What is the equivalent LINQ to SQL query expression of this SQL statement?
- What am I doing wrong in this SQL to LINQ conversion?
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- LINQ C# query in VB - getting an error
- What part of this Linq query is invalid for nHibernate?
- Whats wrong with this LINQ query
- Getting "Could not find an implementation of the query pattern for source type 'ExcelQueryable<T>'. " Error
- Getting wrong output with linq join query
- What would be the LINQ solution for this query in c#?
- What is wrong with this LINQ query?
- Reduce the line of code for this LINQ query
- what am i doing wrong in constructing Linq query
- How to code this LINQ query in a better way
- What is logically wrong about this VB.NET code
- What SQL query or LINQ code would get the following data?
- Getting error in LINQ query to sqlite
- What is the appropriate LINQ query to this specific case?
- What is the performance optimum (or even better coding practise) for writing this Linq query
- LINQ using conditional where generates wrong query
- How to include two object inside the same select on Linq MVC5
- How best to optimise this small bit of c# Linq code
- Grouping by property value using LINQ
- object initializer - setting field to other field doesn't work as expected, why?
- FirstOrDefault() unable to couple with ?? operator
- linq group by contiguous blocks
- How to use orderby for Arraylist?
- C# LINQ .All not working
- Entity Framework 6.1 - how can you query soft deleted records?
- C# LINQ nested select query
- Nested Linq in MVC4 Razor
- Access "_t" element value from LINQ
- LINQ Expression Conversion / Concat from Int to string
- Join issues - three tables
- What would you choose for your data layer today, Linq or Subsonic?
- How to add a where clause to this Entity Framework Linq statement
- How to perform a LINQ GroupBy, Select, and then Take without performance hit?
- asp.net LinqDataSource query multiple tables with join
- Error in a LINQ query