list中,找出某个字段具有最大值的那条记录,怎么写

时间:2022-03-14 00:43:43
例如List.ID,其中最大的ID是100,则要求linq出ID=100的那条记录

请看清楚问题,我是要查询出一条拥有最大值的记录,而不是要获得最大值本身

我的笨办法是先用Max找出最大值,然后再用ID=最大值来查询



是否有更简单的写法呢?

6 个解决方案

#1


int max=list.Max(x=>x.ID)

#2


该回复于2012-06-27 08:52:56被版主删除

#3


前辈请先看清楚问题。

引用 1 楼  的回复:
int max=list.Max(x=>x.ID)

#4


如下,按照降序排序取第一个

public class para
    {
        public int id { get; set; }
        public string  s { get; set; }
    }


 List<para> list = new List<para>() 
                {
                    new para{id=1,s="1"},
                    new para{id=2,s="2"},
                    new para{id=3,s="3"},
                    new para{id=4,s="4"}
                };
                para max_item = list.OrderByDescending(a => a.id).FirstOrDefault();

#5


懒了 到项目代码里去拷的


public string[] GetCallingIdByCallingName(string callingname, string defaultcallingname)
        {
            List<string> callingStrList = new List<string>();
            string[] callings = callingname.Split('/');
            foreach (string callingStr in callings)
            {
                string callingId = null;
                List<Calling> callinglist = GetCallingList();
                if (callinglist.Count <= 0)
                {
                    return callingStrList.ToArray();
                }

                Calling calling = null;
                if (string.IsNullOrEmpty(callingStr))
                {
                    calling = callinglist.Find(c => { return c.CallingName == defaultcallingname; });
                    if (calling != null) callingId = calling.CallingID;
                    callingStrList.Add(callingId);
                    continue;
                }
                List<string> segmentnames = SegmentHelper.DoSegment(callingStr);
                var items = from k in
                                (from c in callinglist
                                 from s in segmentnames
                                 where c.CallingName.Contains(s)
                                 select c)
                            group k by k.CallingID into gc
                            orderby gc.Count() descending
                            select gc.Key;

                if (items.Count() > 0)
                {
                    string key = items.First();
                    callingStrList.Add(key);
                    continue;
                }
            }
            return callingStrList.ToArray();
        }

#6


不好意思,没看清楚,写个全的

public class userList//定义一个userList类
        {
            public int ID { get; set; }
            public string userName { get; set; }
            public userList()
            {
            }
            public userList(int id, string name)
            {
                ID = id;
                userName = name;
            }
        }

想获取最大ID,再用where 
调用方法

              List<userList> list = new List<userList>() 
                {
                    new userList{ID=10,userName="u10"},
                    new userList{ID=100,userName="u100"},
                    new userList{ID=20,userName="u20"},
                    new userList{ID=40,userName="u40"}
                };
            userList max_item = list.Where(x => x.ID == list.Max(y => y.ID)).FirstOrDefault();
            Console.WriteLine(max_item.ID + "\t" + max_item.userName);
/*
100   u100
*/

#1


int max=list.Max(x=>x.ID)

#2


该回复于2012-06-27 08:52:56被版主删除

#3


前辈请先看清楚问题。

引用 1 楼  的回复:
int max=list.Max(x=>x.ID)

#4


如下,按照降序排序取第一个

public class para
    {
        public int id { get; set; }
        public string  s { get; set; }
    }


 List<para> list = new List<para>() 
                {
                    new para{id=1,s="1"},
                    new para{id=2,s="2"},
                    new para{id=3,s="3"},
                    new para{id=4,s="4"}
                };
                para max_item = list.OrderByDescending(a => a.id).FirstOrDefault();

#5


懒了 到项目代码里去拷的


public string[] GetCallingIdByCallingName(string callingname, string defaultcallingname)
        {
            List<string> callingStrList = new List<string>();
            string[] callings = callingname.Split('/');
            foreach (string callingStr in callings)
            {
                string callingId = null;
                List<Calling> callinglist = GetCallingList();
                if (callinglist.Count <= 0)
                {
                    return callingStrList.ToArray();
                }

                Calling calling = null;
                if (string.IsNullOrEmpty(callingStr))
                {
                    calling = callinglist.Find(c => { return c.CallingName == defaultcallingname; });
                    if (calling != null) callingId = calling.CallingID;
                    callingStrList.Add(callingId);
                    continue;
                }
                List<string> segmentnames = SegmentHelper.DoSegment(callingStr);
                var items = from k in
                                (from c in callinglist
                                 from s in segmentnames
                                 where c.CallingName.Contains(s)
                                 select c)
                            group k by k.CallingID into gc
                            orderby gc.Count() descending
                            select gc.Key;

                if (items.Count() > 0)
                {
                    string key = items.First();
                    callingStrList.Add(key);
                    continue;
                }
            }
            return callingStrList.ToArray();
        }

#6


不好意思,没看清楚,写个全的

public class userList//定义一个userList类
        {
            public int ID { get; set; }
            public string userName { get; set; }
            public userList()
            {
            }
            public userList(int id, string name)
            {
                ID = id;
                userName = name;
            }
        }

想获取最大ID,再用where 
调用方法

              List<userList> list = new List<userList>() 
                {
                    new userList{ID=10,userName="u10"},
                    new userList{ID=100,userName="u100"},
                    new userList{ID=20,userName="u20"},
                    new userList{ID=40,userName="u40"}
                };
            userList max_item = list.Where(x => x.ID == list.Max(y => y.ID)).FirstOrDefault();
            Console.WriteLine(max_item.ID + "\t" + max_item.userName);
/*
100   u100
*/