C# 从文件中合并2个或更多列并在c中获得结果#

C# 从文件中合并2个或更多列并在c中获得结果#,c#,C#,我的档案是: outlook temperature Humidity Windy PlayTennis sunny hot high false N sunny hot high true N overcast hot high false P rain mild high false P rain cool normal false P 我从文件中找到了唯一的元素,如下所示: element:occurence suny :2 overcast:1 rain:2 m

我的档案是:

outlook temperature Humidity Windy PlayTennis 

sunny hot high false N

sunny hot high true N

overcast hot high false P

rain mild high false P

rain cool normal false P
我从文件中找到了唯一的元素,如下所示:

element:occurence

suny :2 

overcast:1 

rain:2

mild:1

cool:1

hot :4

normal:1

high:2

false:4

true:1 

n:2

p:3
然后我删除了出现次数小于1的元素

结果如下:

suny : 2
rain: 2
hot :3
high:4
false:4
n:2 
p:3
现在我希望输出为(从第一个输出开始,它应该与每个其他元素循环,形成一个由两个频繁集组成的集合)

这是我的密码:

var occurences = File.ReadAllLines(file)
    .Skip(1)
    .SelectMany(l => l.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(w => w)
    .ToDictionary(g => g.Key, g => g.Count());

foreach(var pair in occurences)
    label1.Text += String.Format("{0}: {1}\n", pair.Key, pair.Value);
我实现这一点是为了找到第一个频繁集

对于第二个,我应该怎么做


我还需要找到第三组。我将从重构开始。1) 创建一个类来保存数据项

public class WeatherForecast
{
    public string Outlook { get; set; }
    public string Temperature { get; set; }
    public string Humidity { get; set; }
    public bool Windy { get; set; }
    public string PlayTennis { get; set; }
}
然后创建你的对象

var forecasts = new List<WeatherForecast>();
foreach(var line in File.ReadLines(file).Skip(1))
{
    var values = line.Split(' ');
    forecasts.Add(new WeatherForecast
                      {
                          Outlook = values[0],
                          Temperature = values[1],
                          Humidity = values[2],
                          Windy = Convert.ToBoolean(values[3]),
                          PlayTennis = values[4]
                      });
}
结果:


需要两个元素的输出。正如我在输出中提到的。请提出建议。是的。这个主意很完美。。但是一个非常愚蠢的问题…在label..i tuk,,var forecast=forecast.GroupBy(f=>new{f.Outlook},(key,group)=>new{Key1=key.Outlook,Count=group.Count()})中表示这个值;由于这里的Dump显示错误(system.collection.generic.ienumerable不包含Dump的定义),所以我们删除了Dump。label1.text=forecast.tostring();--不起作用。。。我应该带字典吗?@BelaKharidia把垃圾扔了。这是特定于Linqpad的。好的。对于printig值,这不起作用。。var forecast=forecast.GroupBy(f=>new{f.Outlook},(key,group)=>new{Key1=key.Outlook,Count=group.Count()});label1.text=forecast.tostring();不能在可枚举项上使用ToString。尝试在可枚举项上使用foreach循环,并使用StringBuilder字符串化每个成员。
var forecasts = new List<WeatherForecast>();
foreach(var line in File.ReadLines(file).Skip(1))
{
    var values = line.Split(' ');
    forecasts.Add(new WeatherForecast
                      {
                          Outlook = values[0],
                          Temperature = values[1],
                          Humidity = values[2],
                          Windy = Convert.ToBoolean(values[3]),
                          PlayTennis = values[4]
                      });
}
//Group by 1 key
forecasts.GroupBy(f => new { f.Outlook }, (key, group) => new { Key1 = key.Outlook, Count = group.Count() });

//Group by 2 keys
forecasts.GroupBy(f => new { f.Outlook, f.Temperature }, (key, group) => new { Key1 = key.Outlook, Key2 = key.Temperature , Count = group.Count()});

//Group by 3 keys
forecasts.GroupBy(f => new { f.Outlook, f.Temperature, f.Humidity }, (key, group) => new { Key1 = key.Outlook, Key2 = key.Temperature, Key3 = key.Humidity, Count = group.Count()});