如何使用LINQ从多个柱面过滤

发布于 2025-01-18 11:13:27 字数 2906 浏览 2 评论 0原文

如何在列表中的字符串数组中搜索项目,其中这些值可以位于多列中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-01-25 11:13:27

使用嵌套列表,您可以使用selectMany折叠这些列表并将其仅转到内部对象。然后,您可以在您指示的两个字段上进行或比较,这样:

example
    .SelectMany(e => e.InnerSources.SelectMany(i2 => i2.InnerSource2s))     
    .Where(e => strtosearch.Contains(e.D1ToSearch) || strtosearch.Contains(e.D3ToSearch));

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:

example
    .SelectMany(e => e.InnerSources.SelectMany(i2 => i2.InnerSource2s))     
    .Where(e => strtosearch.Contains(e.D1ToSearch) || strtosearch.Contains(e.D3ToSearch));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文