如何将带有内连接的查询转换为linq

时间:2018-04-18 15:32:56

标签: c# sql sql-server visual-studio linq

如何在c#代码中转换此SQL

   select t.Message
   from [dbo].[tblA] t
    inner join (
  select Message, max(CreatedDate) as MaxDate
   from [dbo].[tblA]
   group by Message
  ) tm on t.Message = tm.Message and t.CreatedDate = tm.MaxDate
 where  Type='Enter' 
你能帮帮我吗?

我刚创建了这个..但内部连接对我来说更复杂。

         var query = (from each in db.A
                              where each.StartDate == DateTime.Today
                              select each).FirstOrDefault();

2 个答案:

答案 0 :(得分:1)

如果您希望class jobListController: UIViewController { super.viewDidLoad() navigationController?.navigationBar.barTintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0) navigationController?.view.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0) navigationController?.navigationBar.shadowImage = UIImage() navigationItem.title = "JOBS" let searchButton = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(HandleSearch)) let addNewJobButton = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(HandleNewJob)) searchButton.tintColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0) addNewJobButton.tintColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0) navigationItem.rightBarButtonItems = [addNewJobButton, searchButton] } } 中的whole message record获得tblA值,您可以订购结果并选择第一个元素,例如:

max CreatedDate

答案 1 :(得分:0)

根据我的理解,您需要一个类型为Enter的消息,并且您希望获得该消息的Max CreatedDate值。如果这是要求,我认为你不需要内部联接。您可以在LINQ中使用类似下面的内容。

from m in tblA 
where m.Type = "Enter"
group m by m.Message into temp
select new 
{
 Message = temp.Message, 
 CreatedDate = ( from temp2 in temp select temp.CreatedDate).max()
}