如何使用LINQ从多个柱面过滤
如何在列表中的字符串数组中搜索项目,其中这些值可以位于多列中。
using System;
using System.Collections.Generic;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
string[] strtosearch = { "abc", "xyz", "123" };
}
}
我有一个像上面这样的字符串数组,我想从下面的模型中过滤这些值。
public class Source
{
public string S1 { get; set; }
public ICollection<InnerSource> InnerSources { get; set; }
}
public class InnerSource
{
public string D1 { get; set; }
public ICollection<InnerSource2> InnerSource2s { get; set; }
}
public class InnerSource2
{
public string **D1ToSearch** { get; set; }
public string D2 { get; set; }
public string **D3ToSearch** { get; set; }
}
}
这里,前四个值为 D1ToSearch 或 D3ToSearch 的值为 abc 和 xyz 。所以我的查询结果应该限制为前三个条目。
var example = new List<Source>
{
new Source
{
S1 ="1",
InnerSources = new List<InnerSource>
{
new InnerSource
{
D1 ="2",
InnerSource2s= new List<InnerSource2>
{
new InnerSource2
{
D1ToSearch ="abc",
D2="3",
D3ToSearch="222"
},new InnerSource2
{
D1ToSearch ="122",
D2="3",
D3ToSearch="abc"
}
,new InnerSource2
{
D1ToSearch ="abc",
D2="3",
D3ToSearch="333"
}
这里,前四个值为 D1ToSearch 或 D3ToSearch 的值为 abc 和 xyz 。所以我的查询结果应该限制为前三个条目。
,new InnerSource2
{
D1ToSearch ="opq",
D2="3",
D3ToSearch="xyz"
},
,new InnerSource2
{
D1ToSearch ="arstbc",
D2="3",
D3ToSearch="uvw"
} } } } } };
如果 D1ToSearch 或 D3ToSearch 包含上述任何值,我想返回该行。上面是作为 expamle 添加的示例列表。
如何使用 LINQ 查询来做到这一点?
How can I search for an item in the string array in a list where these values can be in multiple columns.
using System;
using System.Collections.Generic;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
string[] strtosearch = { "abc", "xyz", "123" };
}
}
I have a string array like above where I want to filter for these values from the below model.
public class Source
{
public string S1 { get; set; }
public ICollection<InnerSource> InnerSources { get; set; }
}
public class InnerSource
{
public string D1 { get; set; }
public ICollection<InnerSource2> InnerSource2s { get; set; }
}
public class InnerSource2
{
public string **D1ToSearch** { get; set; }
public string D2 { get; set; }
public string **D3ToSearch** { get; set; }
}
}
Here in the first four with values D1ToSearch or D3ToSearch has the value abc and xyz . so my query result should limit to the first three entries.
var example = new List<Source>
{
new Source
{
S1 ="1",
InnerSources = new List<InnerSource>
{
new InnerSource
{
D1 ="2",
InnerSource2s= new List<InnerSource2>
{
new InnerSource2
{
D1ToSearch ="abc",
D2="3",
D3ToSearch="222"
},new InnerSource2
{
D1ToSearch ="122",
D2="3",
D3ToSearch="abc"
}
,new InnerSource2
{
D1ToSearch ="abc",
D2="3",
D3ToSearch="333"
}
Here in the first four with values D1ToSearch or D3ToSearch has the value abc and xyz . so my query result should limit to the first three entries.
,new InnerSource2
{
D1ToSearch ="opq",
D2="3",
D3ToSearch="xyz"
},
,new InnerSource2
{
D1ToSearch ="arstbc",
D2="3",
D3ToSearch="uvw"
} } } } } };
If D1ToSearch or D3ToSearch contains any of the above values I would like to return that row.Above is a sample list added as an expamle .
How can I do this using LINQ query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用嵌套列表,您可以使用
selectMany
折叠这些列表并将其仅转到内部对象。然后,您可以在您指示的两个字段上进行或比较,这样:With nested lists, you can use
SelectMany
to collapse these and get to just the inner objects. Then you can do your OR comparison on the two fields you indicated, like so: