Я хотел бы предложить, чтобы создать пользовательский класс, который может содержать данные, относящиеся к/магазин. Пусть это будет Statisztika
со следующими полями/свойствами: Day
, PersonId
, Visitor
и CountOfVisits
.
Statisztika
определение класса:
public class Statisztika
{
private int iday = 0;
private int ipersonid = 0;
private int ivisitor =0;
private int icount =0;
//class constructor
public Statisztika(string[] twolines)
{
iday = Convert.ToInt32(twolines[0].Split(' ')[0]);
ipersonid = Convert.ToInt32(twolines[0].Split(' ')[1]);
//we have to replace these lines:
//ivisitor = Convert.ToInt32(twolines[1].Split('/')[0]);
//icount = Convert.ToInt32(twolines[1].Split('/')[1].Split(' ')[0]);
//with:
//check for single slash
int pos = twolines[1].IndexOf("/");
if (pos>-1)
{
//in case of error TryParse method returns zero
Int32.TryParse(twolines[1].Substring(0,pos)
.Replace("#", "").Trim(), out ivisitor);
Int32.TryParse(twolines[1].Substring(pos+1,2)
.Replace("#","").Trim(), out icount);
}
}
public int Day
{
get {return iday;}
set {iday = value;}
}
public int PersonId
{
get {return ipersonid;}
set {ipersonid = value;}
}
public int Visitor
{
get {return ivisitor;}
set {ivisitor = value;}
}
public int CountOfVisits
{
get {return icount;}
set {icount = value;}
}
}
Как вы можете видеть, чтобы создать Statisztika
объект, вам нужно передать две строки текста (из файла), чтобы иметь возможность инициировать поля. Теперь нам нужно создать List<Statisztika>
. Представьте себе, что это контейнер данных. Чтобы получить данные из этого списка, мы можем использовать запрос Linq.
Использование:
string sFileName = @"D:\veetel.txt";
string[] alap = File.ReadAllLines(sFileName);
List<Statisztika> stat = new List<Statisztika>();
for(int i=0; i<alap.Length; i+=2)
{
//Linq rules!
string[] twolines = alap.Skip(i).Take(2).ToArray();
//create new Statisztika object and add to list
stat.Add(new Statisztika(twolines));
}
//use create linq query, group data by day and visitor type ;)
var qry = stat
.GroupBy(p=>new{p.Day, p.Visitor})
.Select(grp=>new
{
Day = grp.Key.Day,
Visitor = grp.Key.Visitor,
SumOfVisits = grp.Sum(p=>p.CountOfVisits)
})
.OrderBy(a=>a.Day)
.ThenBy(a=>a.Visitor);
Console.WriteLine("Day Visitor SumOfVisits");
foreach(var sta in qry)
{
Console.WriteLine("{0}\t{1}\t{2}", sta.Day, sta.Visitor, sta.SumOfVisits);
}
Пример вывода:
Day Visitor SumOfVisits
1 0 0
2 0 0
2 1 0
3 0 0
3 2 15
4 0 0
5 0 0
6 0 12
7 0 9
7 1 9
8 0 0
9 0 0
9 1 0
10 0 0
11 0 0
11 1 0
11 3 0
11 13 0
[EDIT]
Примечание: Если \
(слэш) не существует, Statisztika
конструктор класса использует нули в Visitor
и CountOfVisits
полей/членов.
Если вы хотите, чтобы проверить, если код работает правильно, используйте этот код:
string sFileName = @"D:\veetel.txt";
string[] alap = File.ReadAllLines(sFileName);
for(int i=0; i<alap.Length; i+=2)
{
int one = 0;
int two = 0;
string[] twolines = alap.Skip(i).Take(2).ToArray();
int pos = twolines[1].IndexOf("/");
if(pos>-1)
{
Int32.TryParse(twolines[1].Substring(0,pos)
.Replace("#", "").Trim(), out one);
Int32.TryParse(twolines[1].Substring(pos+1,2)
.Replace("#","").Trim(), out two);
}
Console.WriteLine("{0}\t{1}\t{2}",
twolines[1].Substring(0,8), one, two);
}
Приведенный выше код производит вывод следующим образом:
#abor# # 0 0
ta###t## 0 0
0/# a #a 0 0
a pat#k# 0 0
e#zakrol 0 0
1/3#sot# 1 3
0/# a pa 0 0
#3/0#s#t 3 0
verofe#y 0 0
ta#o#t#v 0 0
eszakro# 0 0
#/3#so## 0 3
...etc.
Cheers, Maciej
Ваш вопрос неясна ... что такое * точная * проблема, с которой вы сталкиваетесь? – Sayse
Я не знаю, как сделать День: номер с LinQ –
Ну, как бы вы это сделали без Linq? что вы пробовали/исследовали? – Sayse