如何在 C# 中使用 EF 从 MySQL table 获取第一个和最后一个日期?
How to get first and last date from MySQL table using EF in C#?
这是我获取第一次约会的代码。它有效,我可以在 dateTimepicker 中显示日期。
private void ShowButton_Click(object sender, EventArgs e)
{
String constring = "datasource=;port=3306;username=;password=";
MySqlConnection connection = new MySqlConnection(constring);
int n = Convert.ToInt32(textBox1.Text);
testEntities dc = new testEntities();
var getfirstDate = dc.table.Where(b => b.number == n).First();
dateTimePicker1.Text =Convert.ToString (getfirstDate.date);
}
但是,当我尝试对最后一个日期做同样的事情时
var getlastDate = dc.table.Where(b => b.number == n).Last();
出现一条错误消息:
LINQ to Entities does not recognize the method 'project.table Lasttable' method, and this method cannot be translated into a store expression.
为什么不使用排序?
var getlastDate = dc.table.Where(b => b.number == n).OrderByDescending(b => b.Date).First();
//alternatively to using Where().First();
var getlastDate = dc.table.First(b => b.number == n).OrderByDescending(b => b.Date)
您可以使用 OrderByDescending()
+ First()
:
var lastDate = table.Where(b => b.number == n)
.OrderByDescending(b => b.Date)
.First();
这是我获取第一次约会的代码。它有效,我可以在 dateTimepicker 中显示日期。
private void ShowButton_Click(object sender, EventArgs e)
{
String constring = "datasource=;port=3306;username=;password=";
MySqlConnection connection = new MySqlConnection(constring);
int n = Convert.ToInt32(textBox1.Text);
testEntities dc = new testEntities();
var getfirstDate = dc.table.Where(b => b.number == n).First();
dateTimePicker1.Text =Convert.ToString (getfirstDate.date);
}
但是,当我尝试对最后一个日期做同样的事情时
var getlastDate = dc.table.Where(b => b.number == n).Last();
出现一条错误消息:
LINQ to Entities does not recognize the method 'project.table Lasttable' method, and this method cannot be translated into a store expression.
为什么不使用排序?
var getlastDate = dc.table.Where(b => b.number == n).OrderByDescending(b => b.Date).First();
//alternatively to using Where().First();
var getlastDate = dc.table.First(b => b.number == n).OrderByDescending(b => b.Date)
您可以使用 OrderByDescending()
+ First()
:
var lastDate = table.Where(b => b.number == n)
.OrderByDescending(b => b.Date)
.First();