我有这个问题,我有一个column
in 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();