日期:2014-05-17  浏览次数:20523 次

ling 查询 基础 group by
C# code

 (from p in db.PlayedVideos
 group p.ID by p.VideoID
 into g
 select new {VideoID = g.Key, ID = g.Max()})




------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class PlayedVideo
    {
        public int id { get; set; }
        public int videoid { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<PlayedVideo> playedVideos = new List<PlayedVideo>()
            {
                new PlayedVideo() { id = 1, videoid = 0 },
                new PlayedVideo() { id = 2, videoid = 0 },
                new PlayedVideo() { id = 3, videoid = 0 },
                new PlayedVideo() { id = 4, videoid = 1 },
                new PlayedVideo() { id = 5, videoid = 1 },
                new PlayedVideo() { id = 6, videoid = 1 }
            };
            var query = from x in playedVideos group x.id by x.videoid into g select new { VideoID = g.Key, ID = g.Max() };
            foreach (var item in query)
            {
                Console.WriteLine("VideoID {0}, ID {1}.", item.VideoID, item.ID);
            }
        }
    }
}