0

asp.net アプリケーションがあり、Entity Framework を使用してデータベースに接続しています。このアプリケーションでは、日付を取得するためのテキストボックスがあり (ここではカレンダー css スタイルを使用しています)、文字列型です。

データベースに列があり、その列は日時形式です。テキストボックスの値をデータベースの日付列と比較する必要があります。このため、コードを次のように使用しました

public StudentAttendances(string date)
    {
        if (date != "")
        {
            DateTime date1 = Convert.ToDateTime(date);

            foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
            {
                this.Add(new StudentAttendance(studentAttendance.StudentId));
            }
        }
    }

たとえば、テキスト ボックス (形式は 04/05/2012) で日付を選択し、これをデータベースと比較すると、データは表示されませんが、実際にはこの日付のデータがいくつかあります。

4

3 に答える 3

1

コードは日と時間の両方を比較しています(時間、分などが一致する必要があります)。次のように、1日の部分だけを比較してみてください。

buDataEntities.StudentAttendances.Where(s => s.Date.Subtract(date1).Days == 0)

また、ユーザーからの入力日がどの形式であるかを指定する必要があると思います。2012年4月5日は、コンピューターの地域設定に応じて、4月4日または5月5日の両方を意味する場合があります。これは、アメリカ形式の日付文字列をDateTimeオブジェクトに変換する例(以下)です。

DateTime date1 = DateTime.Parse(date, new CultureInfo("en-US"));

お役に立てば幸いです。

于 2012-04-05T14:44:40.420 に答える
1

以下が機能するかどうかを確認してください。

public StudentAttendances(string date)
{
    if (date != "")
    {
        // please see the change from your given code
        DateTime date1 = DateTime.ParseExact(date, "MM/dd/yyyy",
                                   System.Globalization.CultureInfo.InvariantCulture);

        foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
        {
            this.Add(new StudentAttendance(studentAttendance.StudentId));
        }
    }
}
于 2012-04-05T14:58:05.067 に答える
1

あなたの質問は非常に限られていますが、これを見てみてください

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

于 2012-04-05T12:16:10.990 に答える