为什么我的Linqued查询没有给我正确的结果?

时间:2016-05-09 09:06:09

标签: sql-server linq linq-to-sql sql-to-linq-conversion

我有这个sql查询,它给出了正确的结果,即使用order by子句按A到Z排序的员工的所有名称

select Distinct(EmpNTLogin),Employee from Attendance 
where  CreateDate>='2016-01-01'
order by EmpNTLogin

当我在Linq中转换相同的查询时,我得到了正确的结果,但order by子句不起作用。 这是我的Linqued查询

    var query = (from attendance in db.Attendances
                     orderby attendance.EmpNTLogin
                     where attendance.CreateDate.Value.Year >= 2016
                     select new { attendance.Employee, attendance.EmpNTLogin }).Distinct();

2 个答案:

答案 0 :(得分:5)

在linq查询中,在import UIKit import Foundation class SearchResultViewController: UICollectionViewController, UISearchResultsUpdating { //private let cellComposer = DataItemCellComposer() private var vms: VideoManagerService! private var filteredVideos = [ScoreVideo]() static let storyboardIdentifier = "SearchResultViewController" var filterString = "" { didSet { // Return if the filter string hasn't changed. guard filterString != oldValue else { return } // Apply the filter or show all items if the filter string is empty. if self.filterString.isEmpty { self.filteredVideos = StringSearchService.start(self.filterString, videos: self.vms.videos) } else { self.filteredVideos = StringSearchService.start(self.filterString, videos: self.vms.videos) } self.collectionView?.reloadData() } } override func viewDidLoad() { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate self.vms = appDelegate.getVideoManagerService() } // MARK: UICollectionViewDataSource override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { print("count..\(filteredVideos.count)") return filteredVideos.count } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { // Dequeue a cell from the collection view. return collectionView.dequeueReusableCellWithReuseIdentifier(VideoSearchCell.reuseIdentifier, forIndexPath: indexPath) } // MARK: UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { guard let cell = cell as? VideoSearchCell else { fatalError("Expected to display a `VideoSearchCell`.") } let item = filteredVideos[indexPath.row] cell.configureCell(item.video) } override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { dismissViewControllerAnimated(true, completion: nil) } // MARK: UISearchResultsUpdating func updateSearchResultsForSearchController(searchController: UISearchController) { print("updating... \(searchController.searchBar.text)") filterString = searchController.searchBar.text!.lowercaseString ?? "" } } 之后应用distinct,因此会丢弃该订单。

在调用distinct

之后应用orderby
orderby

答案 1 :(得分:0)

您需要先应用'where'子句,然后应用'orderby'子句。 像这样:

var query = (from attendance in db.Attendances
                 where attendance.CreateDate.Value.Year >= 2016
                 orderby attendance.EmpNTLogin
                 select new { attendance.Employee, attendance.EmpNTLogin }).Distinct();