逗号将值与SQL分隔开,得到不同的值及其计数
本文关键字:SQL 分隔 | 更新日期: 2023-09-27 18:21:52
我在SQL table
中有一个column
时遇到了这个问题。这些值是CCD_ 3格式。
例如:
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();
怎么样:
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()};
通过分组而不是区别,可以获得计数。
类似这样的东西:
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();
- 您需要按名称对元素进行分组
- 在LINQ to Objects.Select()中,运算符可以将lambda中的第二个参数作为索引,这可以帮助您附加计数
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())
试试这个
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);
}