比较LINQ中的日期会产生错误 - 方法无法转换为商店表达式

时间:2015-09-03 07:38:09

标签: c# linq entity-framework

我正试图取出HourRegistrations

HourRegistration.Date == //withint the last 3 months

我正在尝试这样(在网络上找到这个解决方案,但它似乎不起作用)

var dbHourRegs = db.HourRegistration
        .Where(x => DateTime.Compare(x.Date, DateTime.Today.AddMonths(-3)) >= 0)
        .ToList();

但正如标题所说,我一直收到这个错误:

  

LINQ to Entities无法识别方法'System.DateTime AddMonths(Int32)'方法,并且此方法无法转换为商店表达式。

如何进行?或者更确切地说,我如何完成这项任务?

1 个答案:

答案 0 :(得分:4)

mehtod DateTime.Compare是C#方法,无法转换为sql查询。此外DateTime YourDate = DateTime.Now.AddMonths(-3); var dbHourRegs = db.HourRegistration .Where(x => x.Date >= YourDate) .ToList(); 也会因同样的原因而生成错误

你可以这样做

import UIKit

class ViewController: UITableViewController {

var path: String?
var data: String?
var TableView1 = [String]()
var TableView2 = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    self.path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt")
    self.data = String(contentsOfFile:path!, encoding: NSUTF8StringEncoding, error: nil)
    if var content = (data){
        //var line: [String] = content.componentsSeparatedByString("\n")
        var chp: [String] = content.componentsSeparatedByString("#")

        TableView1 += chp
        TableView2 += sec
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return TableView1.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    Cell.textLabel?.text = TableView1[indexPath.row]

    return Cell
}
}