逗号将值与SQL分开,获取不同的值及其计数

时间:2013-07-11 08:47:46

标签: c# linq

我遇到此问题,column中有SQL table。值为comma separated格式。

例如:

 1)   facebook,google,just giving, news letter, in-store
 2)   facebook,google,just giving
 3)   just giving
 4)   facebook
 5)   google
 6)   in-store,email
 7)   email,facebook

现在我想查询该表并在列表中包含不同值及其计数的列表中获取此值。

例如:

1) facebook - 10 
2) email - 20
3) in-store -5
and so on....

无论如何都要使用LINQ来实现这个目标吗?

注意:我可以使用LIST变量上的LINQ运算符,因为我的数据源是entries'不支持LINQ(内置于.NET 2.0)

        var results = new List<string>();
        var builder = new StringBuilder();

        foreach (var entry in entries.Cast<MWCompetitionsEntry>().Where(entry => entry.HowFound != null))
        {
              builder.Append(entry.HowFound).Append(",");
        }

        results.Add(builder.ToString());

我得到的结果

facebook,email,in-store,google,in-store,newsletter,facebook,email,facebok

编辑:

现在,我可以处理results变量。我不知道如何从该变量及其数量中获取唯一值。

请帮助

ANSWER

var values = new List<string>();
            var builder = new StringBuilder();
            var howFoundValues = new List<string>{"Email", "Facebook", "Twitter", "Leaflet", "Just Giving", "In-Store", "Other"};

            foreach (var entry in entries.Cast<MWCompetitionsEntry>().Where(entry => entry.HowFound != null))
            {
                  builder.Append(entry.HowFound).Append(",");
            }

            values.Add(builder.ToString());
            var result1 = values.SelectMany(l => l.Split(','))
                  .Where(howFoundValues.Contains) //will disregard any other items
                  .GroupBy(e => e)
                  .Select(e => String.Format("{0} - {1}",e.Key, e.Count()))
                  .ToList();

4 个答案:

答案 0 :(得分:3)

怎么样:

var result = from word in entries.SelectMany(e => e.HowFound.Split(','))
             group word by word into gr
             select new {Entry = gr.Key, Count = gr.Count()};

分组而不是分开允许您获得计数。

答案 1 :(得分:1)

var entries = new List<string>{"facebook,google,just giving, news letter, in-store",
 "facebook,google,just giving",
 "just giving", 
 "facebook",
 "google",
 "in-store,email",
 "email,facebook"};

var result = entries
.SelectMany (e => e.Split(','))
.GroupBy (e => e).Select (g => string.Format("{0} - {1}", g.Count (), g.Key));

result.Dump();

结果

4 - facebook 
3 - google 
3 - just giving 
1 -  news letter 
1 -  in-store 
1 - in-store 
2 - email 

首先拆分你的条目,然后按不同的字符串分组。

如果您的数据包含前导和/或尾随空格,就像在示例中一样,请考虑使用.GroupBy(e => e.Trim())

答案 2 :(得分:1)

这样的事情:

var groupedResults = results.SelectMany(v => v.Split(','))
                        .GroupBy(n => n)
                        .Select((item, index) => 
                                 new {
                                    name = String.Format("{0}) {1}", index+1, item.Key),
                                    count = item.Count()
                                 })
                        .ToList();
  1. 您需要按名称对元素进行分组。
  2. 在LINQ to Objects中.Select()运算符可以将lambda中的第二个参数作为索引,可以帮助您附加数字。

答案 3 :(得分:1)

试试这个

        Dictionary<string, int> valuesAndCount = new Dictionary<string, int>();
        foreach (var entry in entries) // entries is a collection of records from table
        {
            string[] values = entry.Split(',');
            foreach (var value in values)
            {
                if (!valuesAndCount.ContainsKey(value))
                {
                    valuesAndCount.Add(value, 0);
                }
                valuesAndCount[value] = valuesAndCount[value] + 1;
            }
        }


        //Then you'llhave your distinct values and thei count
        foreach (var key in valuesAndCount.Keys)
        {
            Console.WriteLine("{0} {1}",key, valuesAndCount[key]);
        }

然后字典应包含您的不同值及其计数

LINQ VERSION

        IEnumerable<string> strings = entries
            .ToList()
            .SelectMany(e => e.Split(','))
            .GroupBy(v => v)
            .Select(str => string.Format("{0} {1}", str.Count(), str.Key));


        foreach (var p in strings)
        {
            Console.WriteLine(p);
        }