0

我有这个问题,我有一个columnin 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变量了。我不知道如何从该变量及其计数中获取唯一值。

请帮忙

回答

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

4 回答 4

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()};

分组而不是不同的可以让您获得计数。

于 2013-07-11T08:55:05.747 回答
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 版本

        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);
        }      
于 2013-07-11T09:05:04.897 回答
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())

于 2013-07-11T08:55:06.383 回答
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 中的第二个参数作为索引,这可以帮助您附加编号。
于 2013-07-11T08:55:06.583 回答