Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取列表C#linq中项目的乘积_C#_Linq_Aggregate - Fatal编程技术网

获取列表C#linq中项目的乘积

获取列表C#linq中项目的乘积,c#,linq,aggregate,C#,Linq,Aggregate,我有一个名单,上面有分享代币的IntegerID的人,我试图得到每对朋友id号的乘积。因此,朋友id 2和4将导致8等 var FriendsWhoShareTheMostTokens = FriendsWithTheirSharedTokensAmount.Select(x => new { FriendIdA = x.FriendTo.FriendID, FriendIdB = x.FriendFo

我有一个名单,上面有分享代币的IntegerID的人,我试图得到每对朋友id号的乘积。因此,朋友id 2和4将导致8等

 var FriendsWhoShareTheMostTokens = FriendsWithTheirSharedTokensAmount.Select(x => new
            {
               FriendIdA = x.FriendTo.FriendID,
               FriendIdB  = x.FriendFor.FriendID,
               SharedCountNumber = x.SharedCount
            })
            .Where(x => x.SharedCountNumber == FriendsWithTheirSharedTokensAmount.Max(y => y.SharedCount))
            .ToList();

// need some code here** 
foreach (var pairOffriendsWhoSharetheMostTokens in FriendsWhoShareTheMostTokens)
{  

}
我可以用Linq实现这一点吗?或者实现这一点的最佳方法是什么?

Edit

答案很简单

 var ProductofGreatestTokenSharers = FriendsWhoShareTheMostTokens.Select(x => new
            {
                ProductOfIds = x.FriendIdA * x.FriendIdB
            }
        );

很难理解你想用你的例子实现什么

但是,如果您希望获得id和产品,并且还希望获得最高的
SharedCountNumber

您可能只需执行以下操作

var someResult = someList.Select(x => new
                            {
                                FriendIdA = x.FriendTo.FriendID,
                                FriendIdB = x.FriendFor.FriendID,
                                SharedCountNumber = x.FriendTo.FriendID * x.FriendFor.FriendID    
                            }) // Projection
                          .OrderByDescending(x => x.SharedCountNumber) // order the list
                          .FirstOrDefault(); // get the first

if (someResult != null)
{
    Debug.WriteLine($"A : {someResult.FriendIdA}, B : {someResult.FriendIdB}, Product : {someResult.SharedCountNumber}");
}

这就是我要找的。