猿问

比较2个linq列表中的2个属性,并根据比较结果返回一个布尔值

我试图找到答案,但是似乎没有什么合适的。


要求:从已知的FooObject列表中,返回其数据满足所有搜索条件的Foo Id的列表。


这是我的代码:


class testClass

{

    public class SearchItem

    {

        string Term { get; set; }

        decimal Confidence { get; set; }

    }


    public class FooObject

    {

        public Guid Id { get; set; }

        public List<Data> Data { get; set; }


    }


    public class Data

    {

        public string Text { get; set; }

        public decimal Confidence { get; set; }

    }


    [Test]

    public void Test()

    {

        var searchItems = new List<SearchTerm>

            {

                new SearchTerm{ Confidence = (decimal)1, Term = "TestWord" },

                new SearchTerm{ Confidence = (decimal)1, Term = "TestWord2" },


            };

        var FooObjects = new List<FooObject>

            {

                new FooObject{Id = new Guid(), Data = new List<Data>

                    {                                                                                     

                        new Data{Text = "TestWord", Confidence = 1}, 

                        new Data{Text = "TestWord2", Confidence = 1},                                                                                     

                        new Data{Text = "SomeOtherWord", Confidence = 1},                                                                                  

                    }

            }

        }; 

//result is a list of the Foo IDs

        var result = FooObjects.Where(foo => !searchItems.Select(item => item.Term).Except(foo.Data.Select(dt => dt.Text).Distinct()).Any())

                     .Select(foo => foo.Id).ToList();


        Assert.That(result.Count, Is.EqualTo(1));

        searchItems.Add(new SearchTerm{Term = "NotFoundString"});

        result = FooObjects.Where(foo => !searchItems.Select(item => item.Term).Except(foo.Data.Select(dt => dt.Text).Distinct()).Any())

      .Select(foo => foo.Id).ToList();


        Assert.That(result.Count, Is.EqualTo(0));

    }

}

现在,我需要对此进行修改,以便可以与每个单词的置信度进行比较


问题:如何修改LINQ以将置信度和期限与我的数据进行比较


呼唤远方
浏览 144回答 2
2回答
随时随地看视频慕课网APP
我要回答