如何使用LINQ查询通用列表? [英] How to query generic list using LINQ?

查看:83
本文介绍了如何使用LINQ查询通用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 foreach(setNames中的T setName)
{
List< T> list = objectList.Where(x => x.QualificationID == setName)。ToList();
redisClient.SetAdd< string>(Qualification:+ setName,list.Select(x => x.ProductID).ToString());
}



在第3行中,T不包含'QualificationID'的定义,并且没有扩展方法'QualificationID'接受Type T的第一个参数可能是找到。

类似于第4行,

T不包含'ProductID'的定义,也没有扩展方法'ProductID'接受Type T的第一个参数



我们如何查询泛型类型列表?



我尝试过:



 foreach(setNames中的T setName)
{
List< T> list = objectList.Where(x => x.QualificationID == setName)。ToList();
redisClient.SetAdd< string>(Qualification:+ setName,list.Select(x => x.ProductID).ToString());
}

解决方案

当你使用泛型方法时,它会从集合的类型填充空白 - 所以您可以在每个对象上使用的属性和方法取决于集合的类型。

因此Point对象的集合可以使用Point属性:

列表与LT;点和GT; points = GetPoints(); 
列表<点> leftColumn = points.Where(p = > pX < = 10 )。ToList();

但是字符串集合可以让你使用字符串属性:

 List<串GT; strings = GetStrings(); 
List< string> shortStrings = strings.Where(s = > s.Length < = 10 )。ToList();



无论你的集合被声明为什么,你都可以在课堂上使用哪些属性和方法,而不是集合中的项目可能是。因此,如果您使用基类:

 List< object> strings = GetStrings(); 
List< string> shortStrings = strings.Where(s = > s.Length < = 10 )。ToList();

您将收到错误,因为长度不是<$的属性c $ c> object class,即使集合中的所有对象都是字符串,并且有一个Length - 系统也不会知道类中的所有对象将始终是字符串:

列表< 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();

那么看看 setNames 被声明为 - 它控制你可以使用哪些属性使用


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());
}

解决方案

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:

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:

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:

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:

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.


这篇关于如何使用LINQ查询通用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆