基于多个单元格中的拆分字符串将一行分成许多

发布于 2025-01-18 23:56:34 字数 900 浏览 0 评论 0原文

尝试根据两个单元格中的字符串将一行分成多行。这与问题类似 LINQ 来分隔列值.net 中的一行到不同行 但我需要根据产品和产品进行拆分成本列而不是仅产品列

SNo。产品成本
1高露洁、closeup、pepsodent50、100、150
2rin、surf100

进入

SNo。产品成本
1colgate50
1closeup100
1pepsodent150
2rin100
2surf100

我正在使用 Linq to Object 和 Entity Framework

Trying to split one row into many based on string in two cells. it is similar to the question
LINQ to separate column value of a row to different rows in .net
but i need to split based on Product & Cost Columns rather than product column only

SNo.ProductCost
1colgate,closeup,pepsodent50,100,150
2rin,surf100

into

SNo.ProductCost
1colgate50
1closeup100
1pepsodent150
2rin100
2surf100

I'm using Linq to Object with Entity Framework

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陪我终i 2025-01-25 23:56:34

尝试以下操作。由于您没有提供任何模型,因此名称可能不准确。

var loaded = ctx.Products.ToList();

var query = 
    from p in loaded
    from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
    select new 
    {
        Sno = p.Sno,
        Product = sp.p,
        Cost = sp.c
    };

var splitted = query.ToList(); 

Try the following. Since you have not presented any model it can be inaccurate in names.

var loaded = ctx.Products.ToList();

var query = 
    from p in loaded
    from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
    select new 
    {
        Sno = p.Sno,
        Product = sp.p,
        Cost = sp.c
    };

var splitted = query.ToList(); 
屋檐 2025-01-25 23:56:34

使用@svyatoslavdanyliv命名,这是一个答案:

var loaded = ctx.Products.ToList();

var query = 
    from p in loaded
    from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
    select new 
    {
        Sno = p.Sno,
        Product = sp.p,
        Cost = sp.c
    };

var splitted = query.ToList(); 

对我来说感觉有点复杂。我希望使用扩展方法创建zip的变体,该变体重复较短序列的最后一个元素以匹配较长的序列:

public static class EnumerableExt {
    public static IEnumerable<(T1 First,T2 Second)> ZipExtend<T1,T2>(this IEnumerable<T1> s1, IEnumerable<T2> s2) {
        var s1e = s1.GetEnumerator();
        var s2e = s2.GetEnumerator();

        T1 s1eLast = default;
        T2 s2eLast = default;
        bool has_s2 = false;
        if (s1e.MoveNext()) {
            do {
                s1eLast = s1e.Current;
                if (s2e.MoveNext()) {
                    s2eLast = s2e.Current;
                    has_s2 = true;
                }
                else if (!has_s2)
                    yield break;
                yield return (s1eLast, s2eLast);
            } while (s1e.MoveNext());
            if (has_s2)
                while (s2e.MoveNext())
                    yield return (s1eLast, s2e.Current);
        }

        yield break;
    }
}

那么答案是:

var query = 
    from p in loaded
    from pr in p.Product.Split(',').ZipExtend(p.Cost.Split(','))
    select new 
    {
        Sno = p.Sno,
        Product = pr.First,
        Cost = pr.Second
    };

var splitted = query.ToList();

Using @SvyatoslavDanyliv naming, here is an answer:

var loaded = ctx.Products.ToList();

var query = 
    from p in loaded
    from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
    select new 
    {
        Sno = p.Sno,
        Product = sp.p,
        Cost = sp.c
    };

var splitted = query.ToList(); 

It feels a bit complicated to me. I would prefer using an extension method to create a variant of Zip that repeats the last element of a shorter sequence to match the longer sequence:

public static class EnumerableExt {
    public static IEnumerable<(T1 First,T2 Second)> ZipExtend<T1,T2>(this IEnumerable<T1> s1, IEnumerable<T2> s2) {
        var s1e = s1.GetEnumerator();
        var s2e = s2.GetEnumerator();

        T1 s1eLast = default;
        T2 s2eLast = default;
        bool has_s2 = false;
        if (s1e.MoveNext()) {
            do {
                s1eLast = s1e.Current;
                if (s2e.MoveNext()) {
                    s2eLast = s2e.Current;
                    has_s2 = true;
                }
                else if (!has_s2)
                    yield break;
                yield return (s1eLast, s2eLast);
            } while (s1e.MoveNext());
            if (has_s2)
                while (s2e.MoveNext())
                    yield return (s1eLast, s2e.Current);
        }

        yield break;
    }
}

Then the answer is:

var query = 
    from p in loaded
    from pr in p.Product.Split(',').ZipExtend(p.Cost.Split(','))
    select new 
    {
        Sno = p.Sno,
        Product = pr.First,
        Cost = pr.Second
    };

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