我觉得这很难解释。
我想返回给定 ID 的每一行项目,如果该 ID 包含某些项目。
例如
ID State
1 NY
1 FL
1 NC
2 NY
2 FL
2 GA
2 NC
过滤“FL”、“GA”列表
这将返回
2 NY
2 FL
2 GA
2 NC
不是
2 FL
2 GA
而不是 ID 1 信息,因为它不包含 GA
答案 0 :(得分:0)
假设你有一个看起来像这样的类
public class IdAndState
{
public string Id { get; set; }
public string State { get; set; }
}
您可以创建一些方法,为所有匹配的方法提供 IdAndState
的可枚举性,或其中的可枚举性的可枚举性(取决于您想要的)。
public IEnumerable<IdAndState> GetResultsFlat()
{
return GetResults().SelectMany(x => x);
}
public IEnumerable<IEnumerable<IdAndState>> GetResults()
{
var idsAndStates = new List<IdAndState>();
var states = new List<string>() { "FL", "GA" };
return idsAndStates
// get groups of IdAndStates by ID
.GroupBy(x => x.Id)
// select only the groups where...
.Where(x => states
// ... all of the states are in the IdsAndStates in the group
.All(state => x
.Select(idAndState => idAndState.State)
.Contains(state)))
// make into regular IEnumerable<>
.Select(x => x);
}