Reputation: 45
I have a csv file with the format
LeftChoice, RightChoice, Left || Right
Where LeftChoice and RightChoice are non-unique text strings, and Left || Right is the string that was picked.
What I want to do is get an List that contains for each element: String, Chosen Count, Non Chosen Count.
Currently I have the following:
var result = from line in file
let values = line.Split(',')
select new { Choice1 = values[0], Choice2 = values[1], Result = values[2].Equals("left")};
var leftTrue = from dataSet in result
where dataSet.Result
group dataSet.Choice1 by dataSet.Choice1 into C1
select new { Value = C1.Key, Count = C1.Count() };
var leftFalse = from dataSet in result
where !dataSet.Result
group dataSet.Choice1 by dataSet.Choice1 into C1
select new { Value = C1.Key, Count = C1.Count() };
var rightTrue = from dataSet in result
where !dataSet.Result
group dataSet.Choice2 by dataSet.Choice2 into C2
select new { Value = C2.Key, Count = C2.Count() };
var rightFalse = from dataSet in result
where dataSet.Result
group dataSet.Choice2 by dataSet.Choice2 into C2
select new { Value = C2.Key, Count = C2.Count() };
example input
Fred,Cole,left
Jill,Dave,right
Pat,Fred,right
Pat,Pat,left
example output: would give
Name Chosen NotChosen
Fred 2 0
Cole 0 1
Jill 0 1
Dave 1 0
Pat 1 2
Upvotes: 0
Views: 92
Reputation: 2531
I think the simplest way would be to group by Name and then count the times the name was chosen and not chosen.
var result = from line in file
let values = line.Split(',')
let left = values[0]
let right = values[1]
let leftChosen = values[2].Equals("left")
let pair = new []
{
new { Name = left, IsChosen = leftChosen },
new { Name = right, IsChosen = !leftChosen }
}
from item in pair
group item by item.Name into nameGroup
select new
{
Name = nameGroup.Key,
ChosenCount = nameGroup.Count(x => x.IsChosen),
NonChosenCount = nameGroup.Count(x => !x.IsChosen)
};
Upvotes: 1