[英]How to Filter Two Values inside generic List C# unity?
I don't know what is the better title for this question. 我不知道这个问题的更好的标题是什么。 How to get the data from a generic List using compare between two numbers in generic list ? 如何使用通用列表中两个数字之间的比较从通用列表中获取数据? Using Linq or whatever the effective way. 使用Linq或任何有效的方法。
For example : I have a class database generic list : 例如:我有一个类数据库通用列表:
gems.Add (new gem ("A", 0, 15, 1));
gems.Add (new gem ("B", 16, 46, 2));
gems.Add (new gem ("C", 47, 107, 3));
gems.Add (new gem ("D", 108, 228, 4));
With parameter (Code, time1, time2, gemspent) 带参数(代码,时间1,时间2,宝石花)
What i want is select a gemspent or code between two number from gems generic List. 我想要的是从gems通用列表中选择一个gemspent或两个数字之间的代码。
//------------------------------------------------------------------------------------------------------------------------------------------------------// // ------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -//
Example What i Want : 我想要的例子:
when i input 5 => this will get the "A" in List (this 5 is >= 0 && <= 15 This is inside A range) 当我输入5 =>时,将在列表中获得“ A”(此5为> = 0 && <= 15这在A范围内)
when i input 16 => this will get the "B" in List (this 16 is >= 16 && <= 46 This inside B range) 当我输入16 =>时,它将在列表中获得“ B”(这16是> = 16 && <= 46这个在B范围内)
//------------------------------------------------------------------------------------------------------------------------------------------------------// // ------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -//
What i have try so far : 到目前为止我尝试过的是:
Gem = gDatabase.gems.Where (j => {return j.time1 >= 5 && j.time2 <= 5; }).Select (j => j.gemspent).Sum();
return 0;
Gem = gDatabase.gems.FindIndex(j => {return j.time1 >= 15 && j.time2 <= 15; });
return -1;
Thanks 谢谢
Dennis 丹尼斯
I got my the solution myself, this just a simple mistake that i make. 我自己找到了解决方案,这只是我犯的一个简单错误。
Just change the code below : 只需更改以下代码:
Gem = gDatabase.gems.Where (j => {return j.time1 >= 5 && j.time2 <= 5; }).Select (j => j.gemspent).Sum();
To : 至 :
Gem = gDatabase.gems.Where (j => {return 5 >= j.time1 && 5 <= j.time2; }).Select (j => j.gemspent).Sum();
and 和
Just change the code below : 只需更改以下代码:
Gem = gDatabase.gems.FindIndex(j => {return j.time1 >= 15 && j.time2 <= 15; });
To : 至 :
Gem = gDatabase.gems.FindIndex(j => {return 5 >= j.time1 && 5 <= j.time2; });
That fix it. 修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.