C# 比较2个linq列表中的2个属性,并基于比较返回一个布尔值
我试图找到这个问题的答案,但似乎没有什么是完全正确的 要求:从已知的FooObject列表中,返回其数据满足所有搜索条件的FooID列表 这是我的密码:C# 比较2个linq列表中的2个属性,并基于比较返回一个布尔值,c#,linq,C#,Linq,我试图找到这个问题的答案,但似乎没有什么是完全正确的 要求:从已知的FooObject列表中,返回其数据满足所有搜索条件的FooID列表 这是我的密码: class testClass { public class SearchItem { string Term { get; set; } decimal Confidence { get; set; } } public class FooObject {
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));
}
}
class测试类
{
公共类搜索项
{
字符串项{get;set;}
十进制置信度{get;set;}
}
公共类对象
{
公共Guid Id{get;set;}
公共列表数据{get;set;}
}
公共类数据
{
公共字符串文本{get;set;}
公共十进制置信度{get;set;}
}
[测试]
公开无效测试()
{
var searchItems=新列表
{
新的搜索项{Confidence=(decimal)1,Term=“TestWord”},
新的搜索项{Confidence=(decimal)1,Term=“TestWord2”},
};
var FooObjects=新列表
{
新对象{Id=new Guid(),数据=new List
{
新数据{Text=“TestWord”,置信度=1},
新数据{Text=“TestWord2”,置信度=1},
新数据{Text=“SomeOtherWord”,置信度=1},
}
}
};
//结果是一个Foo id列表
var result=FooObjects.Where(foo=>!searchItems.Select(item=>item.Term)。除了(foo.Data.Select(dt=>dt.Text.Distinct()).Any())
.Select(foo=>foo.Id).ToList();
Assert.That(result.Count,Is.EqualTo(1));
添加(新的SearchTerm{Term=“NotFoundString”});
result=FooObjects.Where(foo=>!searchItems.Select(item=>item.Term)。除了(foo.Data.Select(dt=>dt.Text.Distinct()).Any())
.Select(foo=>foo.Id).ToList();
Assert.That(result.Count,Is.EqualTo(0));
}
}
我现在需要修改这个,这样我可以比较每个单词的可信度
问题:
如何修改LINQ以将置信度和术语与我的数据进行比较也许您正在寻找类似的信息:
var result = FooObjects
.Where(foo => foo.Data.Any(d => searchTerms.Any(
si => d.Term == si.Text && d.Confidence == si.Confidence)))
.Select(foo => foo.Id);
请记住,此搜索无效-如果您的数据集很大,则性能会很差。与其匹配@dymanoid在其回答中所说的任何条件,不如满足所有搜索项/术语(您在示例代码中混合了这些,请保持一致)
更新的例子你可以改写你的要求,这样你就可以清楚地知道你想做什么吗?因为在我看来,你只需要把你的脑袋从盒子里拿出来,把你自己的想法重新表述为well@JanneMatikainen我已经更新了要求,谢谢
var result = FooObjects
.Where(f => searchItems.All(
s => f.Data.Exists(d => d.Text == s.Term && d.Confidence == s.Confidence)))
.Select(f => f.Id);