获取列表C#linq中项目的乘积
我有一个名单,上面有分享代币的IntegerID的人,我试图得到每对朋友id号的乘积。因此,朋友id 2和4将导致8等获取列表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
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}");
}
这就是我要找的。