根据表中的分布/百分比选择正确的组

时间:2013-11-23 22:41:09

标签: c# .net database csv

我有这张CSV表

    filename,set,countrycode,timeofday,calibration,otherconditions,environment,precipitation,sky
    20130913_060749,NULL,AB,day,0.0773734454989712,,city,none,overcast
    20130913_060921,NULL,AB,day,0.289865800606369,,city,"none,haze",overcast
    20130913_060951,NULL,AB,day,0.288528490852974,,city,haze,overcast
    20130913_061021,NULL,AB,day,0.28358887059185,,city,haze,overcast
    20130913_061051,NULL,AB,day,0.190207839896071,,city,haze,overcast
    20130913_061156,NULL,AB,day,0.264707576215636,,city,haze,overcast
    20130913_071226,NULL,AB,day,0.206269488454271,,city,haze,overcast
    20130913_071256,NULL,AB,day,0.24887042174634,,"city,suburb",haze,overcast
    20130913_071326,NULL,AB,day,0.0938082719333332,,suburb,haze,overcast
    20130913_071356,NULL,AB,day,0.00627162842767295,,suburb,haze,overcast
    20130913_073510,NULL,AB,day,0.372935257761037,,city,"fog,haze",partlyCloudy
    20130913_073541,NULL,AB,day,0.328964273325638,,city,fog,partlyCloudy
    20130913_083611,NULL,AB,day,0.289816662996633,,city,fog,partlyCloudy
    20130913_083641,NULL,AB,day,0.291602099474152,,city,fog,partlyCloudy
    20130913_083711,NULL,AB,day,0.205089179739094,,city,fog,partlyCloudy
    20130913_083741,NULL,AB,day,0.345628858397651,,"city,playStreet",fog,partlyCloudy
    20130913_083811,NULL,AB,day,0.0755803723447712,,"city,playStreet",fog,partlyCloudy
    20130913_083958,NULL,AB,day,0.322821196115,,city,fog,partlyCloudy
    20130913_084028,NULL,AB,day,0.191182147849585,,city,fog,partlyCloudy
    20130913_084122,NULL,AB,day,0.348537625962529,,city,fog,partlyCloudy
    20130913_094152,NULL,AB,day,0.331768750001122,,city,"fog,haze",partlyCloudy
    20130913_094222,NULL,AB,day,0.297051596076413,,"city,suburb",haze,partlyCloudy
    20130913_094252,NULL,AB,day,0.430273879317406,,suburb,haze,partlyCloudy
    20130913_094322,NULL,AB,day,0.294162675805556,,suburb,haze,partlyCloudy
    20130913_104352,NULL,AB,day,0,,suburb,haze,partlyCloudy
    20130913_104422,NULL,AB,day,0,,suburb,haze,partlyCloudy
    20130913_104516,NULL,AB,day,0.518891257745526,,suburb,haze,partlyCloudy
    20130913_114546,NULL,AB,day,0.50039745769322,,suburb,haze,partlyCloudy
    20130913_114616,NULL,AB,day,0.275762545494949,,suburb,haze,partlyCloudy
    20130913_114646,NULL,AB,day,0.483892784993604,,suburb,haze,partlyCloudy
    20130913_114716,NULL,AB,day,0.352958331105499,,suburb,haze,partlyCloudy
    20130913_115640,NULL,AB,day,0.363986335968361,,suburb,haze,overcast
    20130913_115710,NULL,AB,day,0.341002657474747,,suburb,haze,overcast
    20130913_115740,NULL,AB,day,0.306984466792517,,suburb,haze,overcast
    20130913_125810,NULL,AB,day,0.468337309183052,,suburb,haze,overcast
    20130913_125840,NULL,AB,day,0.373136910552722,,suburb,haze,overcast
    20130913_125910,NULL,AB,day,0.442251706279878,,suburb,haze,overcast
    20130913_125940,NULL,AB,day,0.473174240569349,,"expressway,suburb",haze,overcast
    20130913_150029,NULL,AB,night,0.0691816235557669,,suburb,fog,undefined
    20130913_150059,NULL,AB,night,0.0434565306952736,,suburb,fog,undefined
    20130913_150654,NULL,AB,night,0.277026709611307,,suburb,fog,undefined
    20130913_150724,NULL,AB,night,0.240319377418189,,suburb,fog,undefined
    20130913_150754,NULL,AB,night,0.209570896148148,,suburb,fog,undefined
    20130913_150824,NULL,AB,night,0.153044032648117,,suburb,fog,undefined
    20130913_150854,NULL,AB,night,0.276118705622896,,suburb,fog,undefined
    20130913_150924,NULL,AB,night,0.0274314977841365,,suburb,fog,undefined
    20130913_151713,NULL,AB,night,0.424058516444064,,suburb,fog,undefined
    20130913_150824,NULL,AB,night,0.153044032648117,,suburb,fog,undefined
    20130913_150854,NULL,AB,night,0.276118705622896,,suburb,fog,undefined
    20130913_150924,NULL,AB,night,0.0274314977841365,,suburb,fog,undefined
    20130913_151713,NULL,AB,night,0.424058516444064,,suburb,fog,undefined
    20130913_150824,NULL,AB,night,0.153044032648117,,suburb,fog,undefined
    20130913_150854,NULL,AB,night,0.276118705622896,,suburb,fog,undefined
    20130913_150924,NULL,AB,night,0.0274314977841365,,suburb,fog,undefined
    20130913_151713,NULL,AB,night,0.424058516444064,,suburb,fog,undefined

我把它读成一个数据表并将它们分组[在上一个问题的帮助下:_)]:

foreach (DataRow row in table.Rows)
{
   var oldRow = row.Field<string>("filename(hhmmss)");
   var newRow = oldRow.Remove(oldRow.Length - 4);
   row.SetField("filename", newRow);
}

var fileNameGroups = table.AsEnumerable()
   .GroupBy(r => r.Field<string>("filename(hhmmss)"));

我希望能够选择满足特定条件(分发)的组:

选择3个满足的组 30%的一天30%的夜晚40%的黎明

PS:是的,上面的表格不是最优的,只是方法和概念证明

任何类似的“选择”示例,欢迎提出问题解决方案

PS&GT;请看下面的评论:)有更多描述

1 个答案:

答案 0 :(得分:0)

然而,奇怪的要求是:

var fileNameGroups = tblCSV.AsEnumerable()
    .GroupBy(r => r.Field<string>("filename(hhmmss)"))
    .Select(g => new
    {
        Group = g, FileName = g.Key,
        DawnPercent = 100.0 * g.Count(r => r.Field<string>("timeofday")=="dawn") / g.Count(),
        NightPercent = 100.0 * g.Count(r => r.Field<string>("timeofday")=="night") / g.Count(),
        DayPercent = 100.0 * g.Count(r => r.Field<string>("timeofday")=="day") / g.Count()
    })
    .Where(x => x.DayPercent == 30 && x.NightPercent == 30 && x.DawnPercent == 40)
    .Select(x => x.Group)
    .Take(3);