I have this issue where i have a column in SQL table. the values are in comma separated format.
ex:
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
Now i want to query that table and get this values in a list where list contains distinct values and their count.
ex:
1) facebook - 10
2) email - 20
3) in-store -5
and so on....
is there anyway to achieve this using LINQ?
NOTE: i can work on LINQ operators on LIST variable because my datasource which is 'entries' does not support LINQ( built in .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());
RESULTS i am getting
facebook,email,in-store,google,in-store,newsletter,facebook,email,facebok
EDITS:
Now, I can work on results variable. i dont know how to get the unique values from that variable and their count.
please help
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();