我在此表中有一个列表
var max = db.Fruits.Max();
我想知道这个表中最常见的水果是什么,这个结果对我来说是什么代码 我正在使用
{{1}}
有错误吗?
答案 0 :(得分:1)
如果您想获得列表中最重要的项目名称,请先找到最常出现的ID:
function loadLikesAndCommentsNotifications (request) {
var currentUser = Parse.User.current();
var query3 = new Parse.Query("Posts");
query3.equalTo("driver", currentUser);
var query4 = new Parse.Query("Posts");
query4.equalTo("passengers", currentUser.get("objectId"));
var main2 = new Parse.Query.or(query3, query4);
var main3 = new Parse.Query("Comments");
main3.matchesKeyInQuery('postId', 'objectId', main2);
var main4 = new Parse.Query("Comments");
main4.equalTo("commentBy", currentUser);
var activityQuery = new Parse.Query.or(main3, main4);
activityQuery.include("commentBy");
activityQuery.limit(15);
return activityQuery.find();
}
Parse.Cloud.define("loadLikesAndCommentsNotifications", function(request, response){
loadLikesAndCommentsNotifications(request).then(response.success, response.error);
});
...
// Similar code for loadRequestNotifications
...
Parse.Cloud.define("loadRequestAndLikesAndCommentsNotifications", function(request, response){
loadLikesAndCommentsNotifications(request).then(function (results) {
var finalResults = { likesAndComments: results};
loadRequestNotifications(request).then(function (results) {
finalResults.request = results;
response.success(finalResults);
}, response.error);
}, response.error);
});
这将返回一个最常见的var fruitAnon = fruits
.GroupBy(item => item.ID)
.Select(item => new {
Key = item.Key,
Count = item.Count()
})
.OrderByDescending(item => item.Count)
.FirstOrDefault();
匿名对象,id
表示它在列表中的存在次数。然后,您可以找到该对象的名称:
count
如果您有这样的列表:
var fruit = fruits.FirstOrDefault(x => x.ID == fruitAnon.Key);
然后:
List<Fruits> fruits = new List<Fruits>() {
new Fruits { ID = 1, Name = "Apple" },
new Fruits { ID = 1, Name = "Apple" },
new Fruits { ID = 2, Name = "Orange" },
new Fruits { ID = 2, Name = "Orange" },
new Fruits { ID = 2, Name = "Orange" },
new Fruits { ID = 2, Name = "Orange" }
};
会打印Console.WriteLine(fruit.Name);
。
答案 1 :(得分:0)
尝试
public class Fruits
{
public int ID { get; set; }
public string Name{ get; set; }
}
var Val = fruitList.GroupBy(x => x.ID,
(key, y) => y.MaxBy(x => x.ID).value)
答案 2 :(得分:0)
正如Drew在评论中所说,你想要GroupBy你关心的值(我做了Name,因为ID在大多数数据结构中往往是唯一的),然后是基于计数的OrderByDescending。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var fruits = new List<Fruit> { new Fruit { ID = 1, Name = "Apple" }, new Fruit { ID = 2, Name = "Apple" }, new Fruit { ID = 3, Name = "Pear" } };
var most = fruits.GroupBy(f => f.Name).OrderByDescending(group => group.Count());
Console.WriteLine(most.First().Key);
}
}
public class Fruit
{
public int ID { get; set; }
public string Name{ get; set; }
}