C#中的LINQ to合并两个分手 [英] C# LINQ to merge two breakup

查看:118
本文介绍了C#中的LINQ to合并两个分手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找简单的LINQ到解决这个问题:

 的String [] =分手新[]
{
    YQ:50 / BF:50 / YR:50,
    YQ:50 / SR:50,
    YQ:50 / BF:50 / YR:50,
    XX:00 ....等等。
};// LINQ前pression字符串expectedResult =YQ:150 / BF:100 / YR:100 / SR:50;

我的另一种解决方法如下:

 公共静态字符串CalcRTBreakup(字符串pstrBreakup)
{
  字符串lstrBreakup = pstrBreakup;
    尝试
    {
        字符串[] = lstrArrRB lstrBreakup.Split('@');
        字符串[] = lstrArrBreakupSplit新的字符串[lstrBreakup.Split('@')[0] .Split('|')长。]
        对于(诠释计数= 0; COUNT< lstrArrRB.Length;计数++)
        {
            字符串[] = lstrArrtemp lstrArrRB [统计] .Split('|');
            对(INT countinner = 0; countinner&下; lstrArrtemp.Length; countinner ++)
            {
                如果(string.IsNullOrEmpty(lstrArrBreakupSplit [countinner]))
                    {
                        如果(string.IsNullOrEmpty(lstrArrtemp [countinner]))
                            继续;
                        lstrArrBreakupSplit [countinner] = lstrArrtemp [countinner]
                    }
                    其他
                    {
                        如果(string.IsNullOrEmpty(lstrArrtemp [countinner]))
                            继续;
                        lstrArrBreakupSplit [countinner] + =/+ lstrArrtemp [countinner]
                    }
                }
            }
            对于(诠释计数= 0; COUNT< lstrArrBreakupSplit.Length;计数++)
            {
                如果(string.IsNullOrEmpty(lstrArrBreakupSplit [计数]))
                    继续;
                lstrArrBreakupSplit [统计] = CalcRTBreakupDict(lstrArrBreakupSplit [计数] .TrimEnd('/'))TrimEnd('/')。
            }
            lstrBreakup =的String.Empty;
            的foreach(在lstrArrBreakupSplit串strtemp)
            {
                lstrBreakup + = strtemp +'|';
        }
        返回lstrBreakup;
    }
    赶上(例外)
    {
        返回;
    }
}
公共静态字符串CalcRTBreakupDict(字符串pstrBreakup)
{
    字符串lstrBreakup = pstrBreakup;
    字典<字符串,双> ldictDreakup =新词典<字符串,双>();
    尝试
    {
        。lstrBreakup = lstrBreakup.TrimEnd('/')修剪();
        字符串[] = lstrArrBreakup lstrBreakup.Split('/');
        的foreach(在lstrArrBreakup串strBr)
        {
            字符串[] lstrBreakup code = strBr.Split(':');
            如果(!ldictDreakup.Keys.Contains(lstrBreakup code [0]))
            {
                双lintTemp = 0; double.TryParse(lstrBreakup code [1],出lintTemp);
                ldictDreakup.Add(lstrBreakup code [0],lintTemp);
            }
            其他
            {
                双lintTemp = 0; double.TryParse(lstrBreakup code [1],出lintTemp);
                lintTemp = lintTemp + ldictDreakup [lstrBreakup code [0];
                ldictDreakup.Remove(lstrBreakup code [0]);
                ldictDreakup.Add(lstrBreakup code [0],lintTemp);
            }
        }
        lstrBreakup =的String.Empty;
        的foreach(在ldictDreakup.Keys串dictKey)
        {
            lstrBreakup + = dictKey +:+ ldictDreakup [dictKey] +/;
        }
        返回lstrBreakup;
    }
    赶上(例外)
    {
        返回pstrBreakup;
    }
}


解决方案

如果您不需要按值排序,你可以简单地做

  VAR解析度=的string.join(/,分手
                              .SelectMany(M = GT; m.Split('/'))
                              。选择(X => x.Split(':'))
                              .GroupBy(M => M [​​0])
                              。选择(M =>的String.Format({0}:{1},m.Key,m.Sum(G => Int32.Parse(G [1])))));

如果你需要订购

  VAR解析度=的string.join(/,分手
                              .SelectMany(M = GT; m.Split('/'))
                              。选择(X => x.Split(':'))
                              .GroupBy(M => M [​​0])
                              。选择(M =>新建
                              {
                                  键= m.Key,
                                  VAL = m.Sum(G => Int32.Parse(G [1]))
                              })
                              .OrderByDescending(M => m.val)
                              。选择(M =>的String.Format({0}:{1},m.key,m.val)));

I am looking for simple LINQ to solve this:

string[] breakups = new[]
{
    "YQ:50/BF:50/YR:50",
    "YQ:50/SR:50",
    "YQ:50/BF:50/YR:50",
    "XX:00 .... and so on"
};

// LINQ expression

string expectedResult = "YQ:150/BF:100/YR:100/SR:50";

My alternate solution as follows

public static string CalcRTBreakup(string pstrBreakup)
{
  string lstrBreakup = pstrBreakup;
    try
    {
        string[] lstrArrRB = lstrBreakup.Split('@');
        string[] lstrArrBreakupSplit = new string[lstrBreakup.Split('@')[0].Split('|').Length];
        for (int count = 0; count < lstrArrRB.Length; count++)
        {
            string[] lstrArrtemp = lstrArrRB[count].Split('|');
            for (int countinner = 0; countinner < lstrArrtemp.Length; countinner++)
            {
                if (string.IsNullOrEmpty(lstrArrBreakupSplit[countinner]))
                    {
                        if (string.IsNullOrEmpty(lstrArrtemp[countinner]))
                            continue;
                        lstrArrBreakupSplit[countinner] = lstrArrtemp[countinner];
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(lstrArrtemp[countinner]))
                            continue;
                        lstrArrBreakupSplit[countinner] += "/" + lstrArrtemp[countinner];
                    }
                }
            }
            for (int count = 0; count < lstrArrBreakupSplit.Length; count++)
            {
                if (string.IsNullOrEmpty(lstrArrBreakupSplit[count]))
                    continue;
                lstrArrBreakupSplit[count] = CalcRTBreakupDict(lstrArrBreakupSplit[count].TrimEnd('/')).TrimEnd('/');
            }
            lstrBreakup = string.Empty;
            foreach (string strtemp in lstrArrBreakupSplit)
            {
                lstrBreakup += strtemp + '|';
        }
        return lstrBreakup;
    }
    catch (Exception)
    {
        return "";
    }
}
public static string CalcRTBreakupDict(string pstrBreakup)
{
    string lstrBreakup = pstrBreakup;
    Dictionary<string, double> ldictDreakup = new Dictionary<string, double>();
    try
    {
        lstrBreakup = lstrBreakup.TrimEnd('/').Trim();
        string[] lstrArrBreakup = lstrBreakup.Split('/');
        foreach (string strBr in lstrArrBreakup)
        {
            string[] lstrBreakupCode = strBr.Split(':');
            if (!ldictDreakup.Keys.Contains(lstrBreakupCode[0]))
            {
                double lintTemp = 0; double.TryParse(lstrBreakupCode[1], out lintTemp);
                ldictDreakup.Add(lstrBreakupCode[0], lintTemp);
            }
            else
            {
                double lintTemp = 0; double.TryParse(lstrBreakupCode[1], out lintTemp);
                lintTemp = lintTemp + ldictDreakup[lstrBreakupCode[0]];
                ldictDreakup.Remove(lstrBreakupCode[0]);
                ldictDreakup.Add(lstrBreakupCode[0], lintTemp);
            }
        }
        lstrBreakup = string.Empty;
        foreach (string dictKey in ldictDreakup.Keys)
        {
            lstrBreakup += dictKey + ":" + ldictDreakup[dictKey] + "/";
        }
        return lstrBreakup;
    }
    catch (Exception)
    {
        return pstrBreakup;
    }
}

解决方案

If you don't need an ordering by value, you can simply do

var res = string.Join("/", breakups
                              .SelectMany(m => m.Split('/'))
                              .Select(x => x.Split(':'))
                              .GroupBy(m => m[0])
                              .Select(m => string.Format("{0}:{1}", m.Key, m.Sum(g => Int32.Parse(g[1])))));

if you need ordering

var res = string.Join("/", breakups
                              .SelectMany(m => m.Split('/'))
                              .Select(x => x.Split(':'))
                              .GroupBy(m => m[0])
                              .Select(m => new
                              {
                                  key = m.Key,
                                  val = m.Sum(g => Int32.Parse(g[1]))
                              })
                              .OrderByDescending(m => m.val)
                              .Select(m => string.Format("{0}:{1}", m.key, m.val)));

这篇关于C#中的LINQ to合并两个分手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆