我有一個在線研究項目,也有很多選擇題。 例如...↓有沒有一種很好的方式來創建一個由linq多重選擇結果的單向表?
問:你喜歡哪種編程語言?
1,C
2,C++
3,CSharp
4,VB.NET
5,JAVA
我會選擇的項目保存爲1和未選定爲0
所以結果是像↓
People Selected
people1 00100
people2 11100
people3 00110
people4 00100
people5 NULL
... ...
現在我的問題是我怎麼可以創建一個單向的表樣下面的結構像上面那樣使用Linq?
TOTAL (N=5)
C 1
C++ 1
CSharp 4
VB.NET 1
JAVA 0
NULL 1
這是我的來源,我認爲這是不好的
static void Main(string[] args)
{
// the mapping...
Dictionary<int, string> dic = new Dictionary<int, string>()
{
{0 , "C"} ,
{1 , "C++"} ,
{2 , "CSharp"} ,
{3 , "VB.NET"} ,
{4 , "JAVA"}
};
// the answer data by the people
List<string> lstM = new List<string>()
{
"00100" , // people1
"11100" , // people2
"00110" , // people3
"00100" , // people4
"NULL" // people5
};
var result = from p in lstM
where "NULL".Equals(p)
group p by p into g
select new {Key = g.Key , Cnt = g.Count()};
foreach (var d in dic)
{
var tmp1 = from p in lstM
where !"NULL".Equals(p) && "1".Equals(p.Substring(d.Key, 1))
select 1;
Console.WriteLine(string.Format("Key = {0} , Cnt={1}", dic[d.Key], tmp1.Count()));
}
foreach (var x in result)
{
Console.WriteLine(string.Format("Key = {0} , Cnt={1}", x.Key, x.Cnt));
}
Console.ReadKey();
}
請任何人都可以給我一些關於它的好主意嗎?
感謝您的意見...
謝謝你,這是非常容易閱讀~~~ – shenhengbin