如何使用LINQ从列表中获取索引 [英] How to grab the index from a list using LINQ

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

问题描述

  lstTeamSales.OrderBy(x => x 。总销售额); 

此列表有一个 int userID 和一个 decimal totalSales



我订购 totalSales 。在那个时候我怎么能弄清楚登录的人的排名?



我知道我可以将用户ID登录的人与列表中的userID的人进行比较。如果他的销售额是#3,我需要返回一个排名第3的排名。

解决方案

问题可以被改写为如何在IEnumerable中获取元素的索引。以下是答案:如何使用LINQ获取索引?
这里是如何使用它:

  int rank = lstTeamSales.OrderBy(x => x.TotalSales).FindIndex(x => x.userID == currentUserID); 

这将比稍微更有效率选择



更新




出现.FindIndex不支持LINQ。任何想法如何实现该功能?



我可能已经想出它现在测试它。我刚刚在ORderBy()之后添加了.ToList()。


No-no-no-no!它杀死了整个想法:(这个想法是添加扩展方法 FindIndex 到IEnumerable,然后使用它。参见示例:

 静态类FindIndexEnumerableExtension 
{
public static int FindIndex< T>(此IEnumerable< T>项,Func< T,bool>谓词)
{
if(items == null )throw new ArgumentNullException(items);
if(predicate == null)throw new ArgumentNullException(predicate);

int retVal = 0;
foreach项目项目
{
if(predicate(item))return retVal;
retVal ++;
}
return -1;
}
}

class YourClass
{
void YourMethod()
{
lstTeamSales.OrderBy(x => x.TotalSales).FindIndex(x => x.UserID == currentUserID);
}
}

定义类 FindIndexEnumerableExtension FindIndex 扩展方法,您可以在代码中的任何地方使用此方法。所有你需要的只是添加使用指令与模块,其中 FindIndexEnumerableExtension 被定义。这是基本上LINQ的工作原理。



如果您不想使用此解决方案,至少在排序之前将lstTeamSales转换为List。并使用 List<> .Sort()方法进行排序。


I have a list I am populating with total sales from a team.

lstTeamSales.OrderBy(x => x.TotalSales);

This list has an int userID and a decimal totalSales.

I order it by totalSales. How can I at that point figure out the rank for the person logged in?

I know I can compare the person who is logged in by his userID to that of the userID in the list. If he is #3 in sales I need to return an int of his rank which would be Rank 3.

解决方案

The question can be rephrased to "How do I get index of element in IEnumerable". Here is the answer: How to get index using LINQ? Here is how to use it:

int rank = lstTeamSales.OrderBy(x => x.TotalSales).FindIndex(x => x.userID == currentUserID);

And this will be slightly more efficient than Select based approaches.

Update

It appears .FindIndex is not supported for LINQ. Any idea how to implement that functionality?

I may have figured it out testing it now. I just added .ToList() after the ORderBy().

No-no-no-no! It kills the whole idea :( The idea is to add extension method FindIndex to IEnumerable. And then use it. See example:

static class FindIndexEnumerableExtension
{
    public static int FindIndex<T>(this IEnumerable<T> items, Func<T, bool> predicate)
    {
        if (items == null) throw new ArgumentNullException("items");
        if (predicate == null) throw new ArgumentNullException("predicate");

        int retVal = 0;
        foreach (var item in items)
        {
            if (predicate(item)) return retVal;
            retVal++;
        }
        return -1;
    }
}

class YourClass
{
    void YourMethod()
    {
        lstTeamSales.OrderBy(x => x.TotalSales).FindIndex(x => x.UserID == currentUserID);
    }
}

After you define class FindIndexEnumerableExtension with FindIndex extension method, you can use this method anywhere in your code. All you need is just add using directive with module where FindIndexEnumerableExtension is defined. This is, basically, how LINQ works.

If you don't want to go with this solution then, at least, convert lstTeamSales to List before sorting it. And sort it using List<>.Sort() method.

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

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