Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (T setName in setNames)
{
 List<T> list = objectList.Where(x => x.QualificationID == setName).ToList();
 redisClient.SetAdd<string>("Qualification:" + setName, list.Select(x => x.ProductID).ToString());
}

In Line 3, T does not contain a definition for 'QualificationID' and no extension method 'QualificationID' accepting a first argument of Type T could be found.
Similarly in Line 4,
T does not contain a definition for 'ProductID' and no extension method 'ProductID' accepting a first argument of Type T could be found

How can we query the list of generic type?

What I have tried:

foreach (T setName in setNames)
{
 List<T> list = objectList.Where(x => x.QualificationID == setName).ToList();
 redisClient.SetAdd<string>("Qualification:" + setName, list.Select(x => x.ProductID).ToString());
}
Posted
Updated 30-Apr-17 21:59pm

1 solution

When you use generic methods, it "fills in the blanks" from the type of the collection - so the properties and methods you can use on each object depends on the type of the collection.
So a collection of Point object can use Point propertuies:
C#
List<Point> points = GetPoints();
List<Point> leftColumn = points.Where(p => p.X <= 10).ToList();
But a collection of strings would let you use string properties:
C#
List<string> strings = GetStrings();
List<string> shortStrings = strings.Where(s => s.Length <= 10).ToList();

Whatever your collection is declared as determines what properties and methods you can use inside your class, not what items in the collection may be. So if you use a base class:
C#
List<object> strings = GetStrings();
List<string> shortStrings = strings.Where(s => s.Length <= 10).ToList();
You will get an error because Length is not a property of the object class, even if all the objects in the collection are strings, and have a Length - the system doesn't "know" that all the objects in the class will always be strings:
C#
List<object> strings = new List<object>();
strings.Add("Hello!");
strings.Add(666);
strigns.Add(new Point(12, 42));
List<string> shortStrings = strings.Where(s => s.Length <= 10).ToList();
So look to what setNames is declared as - that controls what properties you can use.
 
Share this answer
 
Comments
Member 1097736 1-May-17 5:37am    
setNames is an array of type T. T[] setNames .
What can we do in that case?
OriginalGriff 1-May-17 5:42am    
Unless you specify what T can be as part of your generic class / method declaration it is implicitly object - which means you can only use object properties and methods.

Show the line where you first introduce T: the class or method declaration

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900