0

次の json の場合:

{
  "id": "100001","Emp": {"data": [
     {
        "birthday": "08/14/1987", 
        "id": "52442"
      }, 
      {
        "id": "52554"
      }, 
      {
        "id": "54281"
      }, 
      {
        "birthday": "04/08/1986", 
        "id": "54353"
      }, 
      {
        "id": "58667"
      }, 
      {
        "birthday": "12/20", 
        "id": "61435"
      }, 
      {
        "id": "62794"
      }, 
      {
        "id": "64577"
      }, 
      {
        "birthday": "08/12", 
        "id": "65151"
      }, 
      {
        "birthday": "11/15/1988", 
        "id": "66075"
      },
      {
        "birthday": "08/02", 
        "id": "68282"
      }, 
      {
        "birthday": "05/01", 
        "id": "70120"
      }, 
      {
        "birthday": "12/24/1989", 
        "id": "74611"
      }, 
      {
        "birthday": "06/18", 
        "id": "10293"
      }
  ], 
 }
}

一部のEmpオブジェクトにはbirthdayプロパティがなく、一部の誕生日には年が定義されていません。

ここでのタスクは、今日から始まる誕生日を持つ次の 30 個の Emp オブジェクトを取得することです

私のコードはこのようなものです

var response = e.Result;
var jsonData = JsonConvert.DeserializeObject<RootObject>(response);
jsonData.emp.data = jsonData.friends.data.Where(BdayItems => (BdayItems.birthday != null && DateTime.Parse(BdayItems.birthday)<= DateTime.Today.AddDays(30))).OrderBy(BdayItems => BdayItems.birthday).ToList();

.AddYear(-1)また、LINQ クエリで使用して年を削除しようとしました。

しかし、私は次の 30 日を取得していません。すべてのEmp誕生日リストを取得していますが、今日から次の 30 日だけが必要です。

4

3 に答える 3

2

最初に誕生日を現在の年に変換する必要があると思います。生年月日と現在の年との差を追加することで、これを行うことができます。または、うるう年の特定の処理方法が気になる場合は、DateTime コンストラクターと独自のうるう年処理アルゴリズムを使用してください。

また、別の回答として、(今年の)誕生日が現在の日付以降であることを確認する必要があります。

リクエストに応じて、これを行う 1 つの方法の例を次に示します。これは、AddYears のうるう年の処理を使用します。

jsonData.friends.data.Where(BdayItems =>
{
    if (BdayItems.birthday != null)
    {
        var originalBirthday = DateTime.Parse(BdayItems.birthday);
        var today = DateTime.Today; //Storing this prevents a race condition.
        var birthdayThisYear = originalBirthday.AddYears(today.Years - originalBirthday.Years);
        var thirtyDaysFromNow = today.AddDays(30);

        return birthdayThisYear >= today && birthdayThisYear <= thirtyDaysFromNow;
    }

    return false;
});

私は自分の電話を使用しているので、テストしていません。実際のアプリケーションで使用する前に、ロジックが要件に対して正しいことを確認してください。単体テストを使用して、正しく機能することを確認することをお勧めします。

于 2013-07-15T07:52:24.793 に答える
1

また、条件を指定する必要があります。

DateTime.Parse(BdayItems.birthday) >= DateTime.Today

すなわち:

jsonData.friends.data.Where(BdayItems => (BdayItems.birthday != null && 
DateTime.Parse(BdayItems.birthday) >= DateTime.Today &&
DateTime.Parse(BdayItems.birthday)<= DateTime.Today.AddDays(30))).OrderBy(BdayItems => BdayItems.birthday).ToList();
于 2013-07-15T07:47:01.920 に答える
0

あなたが抱えている問題はDateTime、空の年を説明できないことです。年を指定しない場合は、現在の年であると見なされます。あれは:

 string stringDate = "05/01";
 int year = DateTime.Parse(stringDate).Year; // -> 2013

また、日付が空の場合、とにかく特定の日付を占めます。

したがって、問題に対処するためにできる最善のことは、日付ではなく文字列を考慮することです。すべての情報が完成したら、日付の検討を開始します (日、月、年)。

これに加えて、クエリは必要な種類の結果を提供しません (ただし、他の回答がこの問題を既に処理しています)。

于 2013-07-15T07:50:35.260 に答える