oftype

LINQ: From a list of type T, retrieve only objects of a certain subclass S

怎甘沉沦 提交于 2019-12-17 10:53:10
问题 Given a simple inheritance hierarchy: Person -> Student, Teacher, Staff Say I have a list of Persons, L. In that list are some Students, Teachers, and Staff. Using LINQ and C#, is there a way I could write a method that could retrieve only a particular type of person? I know I can do something like: var peopleIWant = L.OfType< Teacher >(); But I want to be able to do something more dynamic. I would like to write a method that will retrieve results for any type of Person I could think of,

Filtering the object of a type with OfType in C#

荒凉一梦 提交于 2019-12-09 19:02:50
问题 I have a base class Base, and class A/B that inherits from it. public class Base { int x; } public class A : Base { int y; } public class B : Base { int z; } I tried to use OfType to filter the only object that I need as follows: public static void RunSnippet() { Base xbase; A a; B b; IEnumerable<Base> list = new List<Base>() {xbase, a, b}; Base f = list.OfType<A>; // I need to get only the object A Console.WriteLine(f); } When I compiled the code, I got this error: error CS0428: Cannot

How Does Queryable.OfType Work?

拈花ヽ惹草 提交于 2019-12-05 00:51:34
问题 Important The question is not "What does Queryable.OfType do , it's "how does the code I see there accomplish that?" Reflecting on Queryable.OfType, I see (after some cleanup): public static IQueryable<TResult> OfType<TResult>(this IQueryable source) { return (IQueryable<TResult>)source.Provider.CreateQuery( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof(TResult) }) , new Expression[] { source.Expression })); } So let me see if I've

Filtering the object of a type with OfType in C#

浪子不回头ぞ 提交于 2019-12-04 14:08:30
I have a base class Base, and class A/B that inherits from it. public class Base { int x; } public class A : Base { int y; } public class B : Base { int z; } I tried to use OfType to filter the only object that I need as follows: public static void RunSnippet() { Base xbase; A a; B b; IEnumerable<Base> list = new List<Base>() {xbase, a, b}; Base f = list.OfType<A>; // I need to get only the object A Console.WriteLine(f); } When I compiled the code, I got this error: error CS0428: Cannot convert method group 'OfType' to non-delegate type 'Base'. Did you intend to invoke the method? What's wrong

How Does Queryable.OfType Work?

余生长醉 提交于 2019-12-03 15:41:56
Important The question is not "What does Queryable.OfType do , it's "how does the code I see there accomplish that?" Reflecting on Queryable.OfType, I see (after some cleanup): public static IQueryable<TResult> OfType<TResult>(this IQueryable source) { return (IQueryable<TResult>)source.Provider.CreateQuery( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof(TResult) }) , new Expression[] { source.Expression })); } So let me see if I've got this straight: Use reflection to grab a reference to the current method (OfType). Make a new method,

How to filter to all variants of a generic type using OfType<>

假如想象 提交于 2019-12-01 05:21:29
I want to filter objects in a List<ISeries> using their type, using OfType<>. My problem is, that some objects are of a generic interface type, but they do not have a common inherited interface of their own. I have the following definitions: public interface ISeries public interface ITraceSeries<T> : ISeries public interface ITimedSeries : ISeries //and some more... My list contains all kinds of ISeries , but now I want to get only the ITraceSeries objects, regardless of their actually defined generic type parameter, like so: var filteredList = myList.OfType<ITraceSeries<?>>(); //invalid

How to filter to all variants of a generic type using OfType<>

时光总嘲笑我的痴心妄想 提交于 2019-12-01 02:27:26
问题 I want to filter objects in a List<ISeries> using their type, using OfType<>. My problem is, that some objects are of a generic interface type, but they do not have a common inherited interface of their own. I have the following definitions: public interface ISeries public interface ITraceSeries<T> : ISeries public interface ITimedSeries : ISeries //and some more... My list contains all kinds of ISeries , but now I want to get only the ITraceSeries objects, regardless of their actually

LINQ: From a list of type T, retrieve only objects of a certain subclass S

混江龙づ霸主 提交于 2019-11-27 13:28:36
Given a simple inheritance hierarchy: Person -> Student, Teacher, Staff Say I have a list of Persons, L. In that list are some Students, Teachers, and Staff. Using LINQ and C#, is there a way I could write a method that could retrieve only a particular type of person? I know I can do something like: var peopleIWant = L.OfType< Teacher >(); But I want to be able to do something more dynamic. I would like to write a method that will retrieve results for any type of Person I could think of, without having to write a method for every possible type. you can do this: IList<Person> persons = new List