简体   繁体   English

如何在具有多个表的 linq 中使用 join 并使用 orderby?

[英]How to make use of join in linq having multiple tables and use orderby?

I need some help, i have a one working join, need the other one for third table?我需要一些帮助,我有一个工作连接,需要另一个用于第三张表吗? How can i create it?我怎样才能创建它? My orderby does not work either with year and need some help also.我的 orderby 也不适用于 year 并且还需要一些帮助。 This is my logic as below and using Linq in sql;这是我的逻辑如下,并在 sql 中使用 Linq;

 // controller
      public IList<ExtractionViewModel> GetExtractionViewModels()
            {
                ProductionManagementEntities db = new ProductionManagementEntities();
    
                var scheduleList = (from p in db.ProductionDays
                                    join w in db.Weeks on p.WeekId equals w.WeekId
// need other join here for the second table
                                    orderby w.Year ascending // this is not working, year starts in 2017 instead of 2021 downwards
                                    where(w.WeekNum == 9)
                                    
                                  
                                    select new ExtractionViewModel
                                  {
    
                                   Year = w.Year,
                                   Week = w.WeekNum,
                                   Day = p.ProductionDate,
    
    
    
                                    }).ToList();
                return scheduleList;
            }
// Model
  public class ExtractionViewModel
    {
        public string Year { get; set; }

        public int Week { get; set; }

        public DateTime Day { get; set; }

        public string VW250 { get; set; }

        public string VW270 { get; set; }

        public string VW250_2PA { get; set; }

        public string VW_270_PA { get; set; }
    }

//Controller 


public IList<ExtractionViewModel> GetExtractionViewModels()
        {
            ProductionManagementEntities db = new ProductionManagementEntities();

            var scheduleList = (from p in db.ProductionDays
                                from m in db.Models
                                join w in db.Weeks on p.WeekId equals w.WeekId
                                orderby w.Year descending
                                orderby m.Name ascending
                                where(m.Name== "VW250")
                                where(w.WeekNum == 9)
                                
                              
                                select new ExtractionViewModel
                              {

                                Year = w.Year,
                               Week = w.WeekNum,
                               Day = p.ProductionDate,
                               VW250 = m.Name


                                }).ToList();
            return scheduleList;
        }

Using a Linq query, this should work:使用 Linq 查询,这应该工作:

var scheduleList = (from p in db.ProductionDays
                    join w in db.Weeks on p.WeekId equals w.WeekId
                    join n in db.NewTable on p.WeekId equals n.WeekId
                    where w.WeekNum equals 9 and m.Name equals "VW250"
                    orderby w.Year ascending
                    select new ExtractionViewModel
                    {
                        Year = w.Year,
                        Week = w.WeekNum,
                        Day = p.ProductionDate,
                        Property = n.Property
                    }).ToList();

A simpler way, and also seems to run a bit quicker as well, is:一种更简单的方法,并且似乎也运行得更快一些,是:

var scheduleList = db.ProductionDays
                   .Include(x => x.Weeks)
                   .Include(x => x.NewTable)
                   .Where(x => x.Week.WeekNum == 9)
                   .OrderBy(x => x.Week.Year)
                   .Select(x => new ExtractionViewModel {
                         x.Week.Year,
                         x.Week.WeekNum,
                         x.ProductionDate,
                         x.NewTable.Property
                    })
                    .ToList();

The second one is linq method and when debugging and stepping through I notice that they seem to be quicker than linq queries.第二个是 linq 方法,在调试和单步执行时,我注意到它们似乎比 linq 查询更快。

The problem with your query seemed to be the syntax on the where clause.您的查询的问题似乎是where子句的语法。 You had where(w.WeekNum == 9) which may work but I have never seen that syntax.你有where(w.WeekNum == 9)可能有效,但我从未见过这种语法。 With linq, I have only worked with lambda expressions or where property equals value type syntax.使用 linq,我只使用了 lambda 表达式或where property equals value类型语法。 I haven't tested this so if there is an error you will probably need to move the .OrderBy() statement to the bottom, but it should be fine.我没有对此进行测试,因此如果出现错误,您可能需要将.OrderBy()语句移至底部,但应该没问题。

In the question you don't mention what third table you would like to join but indicate that there is a third table.在问题中,您没有提及您要join的第三张桌子,而是指出有第三张桌子。 I added NewTable and NewTable.Property to indicate the third table and one of its columns/Properties.我添加了NewTableNewTable.Property来指示第三个表及其列/属性之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM