【问题标题】:Nested Select in Join and Partition By连接和分区中的嵌套选择
【发布时间】:2018-12-20 14:22:26
【问题描述】:

我是 C# 的新手,我想在 Linq.plz 帮助中编写这个 TSql 代码。谢谢

select a.Id,
       a.Date,
       b.Title CategoryTitle,
       a.Title,
       a.Description,
       a.Image 
  from (select *, 
               ROW_NUMBER() over(partition by CategoryID order by Date) rankno 
          from News) a
  join Categories b on a.CategoryID = b.Id
 where rankno <= 5

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试
  • from n in News orderby n.Date group new {n} by new {n.CategoryID} into g select g
  • 如果你只想要前5个项目,你可以使用myData.Take(5)。否则,如果不了解您的数据库结构以及您想要实现的目标,此类问题将很难回答。
  • 我的 tsql 显示我想要的,但我想从新闻模型的任何类别中获得前 5 名。我有一个保存新闻的模型和一个保存类别的模型。任何新闻都有一个类别。
  • Convert SQL to LINQ Query的可能重复

标签: c# linq tsql


【解决方案1】:

假设您有新闻和类别的枚举:

var results = News.Join(Categories,         // Join News and Categories
                        a => a.CatergoryId, 
                        b => b.Id, 
                        (a,b) => new { News = a, Category = b}
                    )  
    .GroupBy(c => c.Category) // "partition by categoryId"
    .SelectMany(g => g.OrderBy(gd => gd.News.CreationDate)   // "order by Date"
                        .Take(5)    // RankNo <= 5
                        .Select(gdd => new {            // results
                                Id = gdd.News.Id, 
                                Date = gdd.News.Date, 
                                CategoryTitle = gdd.Category.Title,
                                Title = gdd.News.Title,
                                Description = gdd.News.Description, 
                                Image = gdd.News.Image
                            })
                );

【讨论】:

    【解决方案2】:

    我使用类为您的数据库建模以确保语法正确。见下面的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication93
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                Context dbContext = new Context();
    
                var results = (from n in dbContext.news 
                               join c in dbContext.category.OrderBy(x => x.Date) on n.Id equals c.Id 
                               select new { news = n, category = c})
                               .Select((x,i) => new { Date = x.news.Date, CategoryTitle = x.category.Title, Title = x.news.Title, Description = x.news.Description, Image = x.news.Image, RankNo = i})
                               .ToList();
    
    
            }
        }
    
        public class Context
        {
            public List<News> news { get; set; }
            public List<Category> category { get; set; }
        }
        public class News
        {
            public int Id { get; set; }
            public DateTime Date { get; set; }
            public string Title { get;set;}
            public string Description { get;set;}
            public byte[] Image { get;set;}
    
        }
        public class Category
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public DateTime Date { get; set; }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多