score:148
you can use cast<t>()
or oftype<t>
to get a generic version of an ienumerable that fully supports linq.
eg.
ienumerable objects = ...;
ienumerable<string> strings = objects.cast<string>();
or if you don't know what type it contains you can always do:
ienumerable<object> e = objects.cast<object>();
if your non-generic ienumerable
contains objects of various types and you are only interested in eg. the strings you can do:
ienumerable<string> strings = objects.oftype<string>();
score:4
you can also use linq's query comprehension syntax, which casts to the type of the range variable (item
in this example) if a type is specified:
ienumerable list = new arraylist { "dog", "cat" };
ienumerable<string> result =
from string item in list
select item;
foreach (string s in result)
{
// invalidcastexception at runtime if element is not a string
console.writeline(s);
}
the effect is identical to @jaredpar's solution; see 7.16.2.2: explicit range variable types in the c# language specification for details.
score:13
yes it can. you just need to use the cast<t>
function to get it converted to a typed ienumerable<t>
. for example:
ienumerable e = ...;
ienumerable<object> e2 = e.cast<object>();
now e2
is an ienumerable<t>
and can work with all linq functions.
Source: stackoverflow.com
Related Query
- Does LINQ work with IEnumerable?
- How does LINQ expression syntax work with Include() for eager loading
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- Order by does not work with Concat() in LINQ
- My Enumerable class does not work with Linq statements like .where in c#
- Why Linq Prepend() does not work with List<T>?
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- Why does DbFunctions not work with Linq to Entities
- C# HashSet union on IEnumerable within LINQ Func expression does not work (possible precompiler bug)
- Linq query with MongoDB works does not work as Func
- How does a LINQ IEnumerable work under the hood?
- Left join with linq query does not work with null columns in datatable
- using datetime.add(timespan) with linq but does not work
- LINQ to SQL does not work properly with Automapper
- Why does this LINQ statement return null and not a IEnumerable with count=0
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- Linq to xml with missing nodes in the source XML and null-coalescing operator won't work
- What does this C# code with an "arrow" mean and how is it called?
- C# 6 null conditional operator does not work for LINQ query
- how does except method work in linq
- How does OrderBy work with regard to strings in C#?
- How does Linq work (behind the scenes)?
- Exclude types form IEnumerable with linq
- Does 'And' vs 'AndAlso' matter with linq in vb.net?
- Does this LINQ code perform multiple lookups on the original data?
- Dynamically cast IEnumerable to IQueryable or dynamically call AsQueryable with LINQ Expressions
- How do I convert IEnumerable to Collection with linq
- Splice IEnumerable with Linq
- Nhibernate LINQ DateTime.AddDay does not work
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
More Query from same tag
- Linq conditional query in C#
- Why cannot use LINQ on DataRowCollection if its base class derives from IEnumerable<T>
- Is there a dumb "all" in linq that evaluate all element?
- Fetch value of element by attribute using linq to xml
- Filtering on template list with property name as string
- C# merging two object lists
- Repository taking linq expression for filtering
- ASP MVC Paging with LINQ?
- How can I use a view of my database in a razor view?
- How do I display total runtime in SqlServer Management Studio 2005?
- Bind a LINQ query to FastReport and get only one row of data
- Reset(not removing) property for duplicates in a list using linq
- Linq select from list where property matches a condition
- Write List<KeyValuePair<string, List<string>>> to text file?
- How to check if all elements of a list are different
- Dynamic LINQ Query calculate percentage withing the LINQ C#
- Remove same value in Lists within a Dictionary in C# - LINQ
- How can apply a group by to a result of another group by using MongoDb?
- Performance Linq
- JSON.NET Cast Exception on DeserializeObject()
- EF deferred execution using SQLquery as input
- Property reflection inside of a LINQ query doesn't behave as expected
- Check if each item in a list belongs to certain group and create a new list
- Wrapped Entity Framework - Unable to create a constant value of type x. Only primitive types are supported in this context
- SelectListItem and linq any and select statement
- Adding fields that can contain null together in lambda/linq
- Error 1 'MatankProj.DB.GeneralDB' does not implement interface member 'MatankProj.Entities.IEntity.PopulateStock(System.Data.DataRow)'
- Best Practices: EfCore/Linq - Single vs SingleOrDefault - Better exception messages
- Getting Last rows from the result of Linq to Sql statement
- Order a list of objects by value match in another list