score:1
Accepted answer
Change your method like below
public void MyProblemMethod<T>(IEnumerable<T> allData, string officeToFind) where T : MyBaseClass
{
// How do I tell Linq that my <T> type is derived from 'MyBaseClass' so I can access the 'OfficeName' property?
IEnumerable<T> myData = allData.Where(c => c.OfficeName .ToLower().Equals(officeToFind.ToLower()));
MyUsefulObject.DataSource= myData; // This needs to have access to the properties in 'MyStronglyTypedResultSet'
MyUsefulObject.DataaBind();
}
score:2
You can use the OfType extension method.
public void MyProblemMethod<T>(IEnumerable<T> allData, string officeToFind)
{
// How do I tell Linq that my <T> type is derived from 'MyBaseClass' so I can access the 'OfficeName' property?
IEnumerable<T> myData = allData.OfType<MyBaseClass>.Where(c => c.OfficeName .ToLower().Equals(officeToFind.ToLower()));
MyUsefulObject.DataSource= myData;
MyUsefulObject.DataaBind();
}
Source: stackoverflow.com
Related Articles
- Generic method using Linq and inheritance to bind to object
- Cast Entity to Implemented Interface in a Generic Method Using LINQ for Entity Framework
- Execute method for each object in generic list using lambda
- Call Ignore Case for Contains Method using a generic LINQ Expression
- Generic Extension method for LINQ JOIN using 2 columns
- Using Linq to merge all List returned by the method of an object in a List
- How to implement generic method approach using Linq and XML
- Make a Search Method generic using LINQ
- Custom LINQ provider calling an asynchronous, generic method with reflection fails when getting the Task.Result object
- Bind distinct data to repeater using linq to object distinct
- How to instantiate object from string using Linq New method and provide a constructor parameter?
- LINQ querying using an entity object not in the database - Error: LINQ to Entities does not recognize the method
- LINQ where clause using Generic IQueryable source
- Get records from Generic Object using LINQ C#
- Need to build a generic query method using LINQ expressions
- C#, LINQ a generic sort method to sort a list by object properties and nested properties
- c# WPF bind combobox to TPH in Entity Framework code first using LINQ
- c# linq to entities using method based queries - trying to select where the object appears only once
- How to update a particular field of a particular object in Generic list in c#.net using LINQ
- Generic method using linq expressions
- Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Using LINQ extension method syntax on a MatchCollection
- How to get first object out from List<Object> using Linq
- Return list of specific property of object using linq
- Ambiguous call when using LINQ extension method on DbSet<T>
- Get index of object in a list using Linq
- Using LINQ for an object that only has GetEnumerator()
- Using IDisposable object in method that returns IEnumerable<T>
- c# to vb.net linq select
- Having trouble understanding methods parameters
- How to display the following result in WPF chart
- ViewBag on ListBox gets empty data
- 'string' does not contain a definition for 'Contains'
- What is the max LINQ Expression Trees can do?
- How to update an object in a c# HashSet?
- Removing Items from a list of objects when an object property is duplicated
- How to convert given data into IEnumerable object
- linq to entities generated sql
- How to GroupBy using LINQ query in C# when the key is to be the same type as your type T when grouping List<T>
- Using Linq for copying/filtering produces a dynamic result in foreach loops?
- Lambda Expression - is it evaluating correctly?
- C#: Linq update without checking constraints and foreign key
- LINQ to a class in a group in a list
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Query Linq to Sql Entity Framework c#
- default values in LINQ query
- Is maintain the transaction with a static LINQ to SQL DataContext in asp.net possible?
- Connect to several SQL Servers