0

私は、Except 演算子と比較を使用して Linq で比較する方法の例をいくつか見てきましたが、それらはすべて、2 つの単純な型または 1 つの単純な型と 1 つの複合体でどのように行われるかを示しているように見えました。異なるタイプの 2 つのリストがあります。子プロパティに基づいて結果を選択し、プロパティが一致しているDateTimeがより新しい別のグループを選択する必要があります。誰でもこれで私を助けることができますか?

        public class Parent
        {
            public List<Child> ChildList;
        }

        public class Child
        {
            public string FoodChoice;
            public DateTime FoodPick;
        }


        public class Food
        {
            public string FoodName;
            public DateTime FoodPick;
        }

        public void FoodStuff
    {
       var parent = new Parent();
     var childList = new List<Child>();
childList.Add( new Child {FoodChoice="a",DateTime=..... 
childList.Add( new Child {FoodChoice="b",DateTime=..... 
childList.Add( new Child {FoodChoice="c",DateTime=..... 
parent.ChildList = childList;
        var foodList = new List<Food>();
        foodList.Add......
        var childrenWithNoMatchingFoodChoices = from ufu in Parent.ChildList where !Parent.ChildList.Contains ( foodList.FoodName )
        var childrenWithMatchingFoodChoicesButWithNewerFoodPick = from foo in Parent.ChildList where Parent.ChildList.FoodPick > foodList.FoodPick
    }

List<Child>forを取得する方法を理解しようとしていchildrenWithNoMatchingFoodChoicesます。List<Child>forを取得する方法を理解しようとしていますchildrenWithMatchingFoodChoicesButWithNewerFoodPick

ヘルプ?.NET Framework 4.0 を使用しています。

ありがとう。

4

1 に答える 1

1

FoodChoiceがfoodListにない子のリストを取得するには、次のクエリを使用します。

var childrenNoMatch = parent.ChildList
                 .Where(ch => !foodList.Any(f => f.FoodName == ch.FoodChoice));

次に、これらの線に沿って何かを試してみます。

    var childrenMatch = parent.ChildList.Except(childrenNoMatch);

    //childrenWithMatchingFoodChoicesButWithNewerFoodPick
    var moreRecent = from ch in childrenMatch
             let food = foodList.First(f => f.FoodName == ch.FoodChoice)
             where DateTime.Compare(ch.FoodPick, food.FoodPick) == 1
             select ch

ただし、テストされていません。

于 2012-05-01T18:23:24.420 に答える