[Serializable()]
public struct FileRecord
{
public int FN { get; set; }
public int SN { get; set; }
public int BN { get; set; }
public const string ADD = "ADD BRD:";
public const string BP = "BACK";
public const string BT = "E32";
public const string HBT = "ME32";
public const string BS = "LOADSHARE";
public const string ADS = "ACTIVE";
public FileRecord(int fn, int sn, int bn) : this()
{
FN = fn;
SN = sn;
BN = bn;
}
}
public class Data
{
public string FileName { get; set; }
public Data(string fileName)
{
this.FileName = fileName;
}
private Dictionary<int, FileRecord> AddValues(int startDictionaryKey)
{
return this.SelectCorrectionInformation().ToDictionary(key => startDictionaryKey++);
}
public IEnumerable<string> PrintValues()
{
var dic = this.AddValues(0);
foreach (KeyValuePair<int, FileRecord> pair in dic)
{
yield return String.Format("FN = {0}, SN = {1}, BN = {2}",
pair.Value.FN,
pair.Value.SN,
pair.Value.BN);
}
}
private IEnumerable<FileRecord> SelectCorrectionInformation()
{
var buf = File.ReadAllLines(this.FileName);
foreach (var i in buf)
{
var data = new FileRecord(
GetNoFixedValue(i, 0),
GetNoFixedValue(i, 1),
GetNoFixedValue(i, 6));
yield return data;
}
}
private int GetNoFixedValue(string input, int index)
{
return Convert.ToInt32(this.GetNumbersFrom(input.Split(',')[index]));
}
private string GetNumbersFrom(string input)
{
return new String(input.Where(value => "0123456789".Contains(value)).ToArray());
}
Использование:
static void Main(string[] args)
{
Data data = new Data("TextFile1.txt");
var values = data.PrintValues();
foreach (var i in values)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
Результат:
FN = 1, SN = 2, BN = 9
FN = 1, SN = 3, BN = 16
FN = 1, SN = 15, BN = 3
из, которого хочу создать объекты со значениями FN=, SN=, BN= и вывести их с помощью такого кода:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
public partial class _class : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] umg_conf = System.IO.File.ReadAllLines(@"c:\mml.txt");
foreach (string name in umg_conf)
{
if (name.Contains("ADD BRD: ") == true)
{
if (name.Contains(" BP=BACK, BT=E32, HBT=ME32, BS=LOADSHARE, "))
{
string[] stringArray = name.Split(',');
IEnumerable<board_me32> queryNames =
from strItem in stringArray
let x=stringArray
select new board_me32()
{
board_no = x[6].Substring(4),
sl_no=x[1].Substring(4),
Fr_no=x[0].Substring(12)
};
List<board_me32> all_boards = queryNames.ToList();
poisk(all_boards);
}
}
}
}
public void poisk(params object [] all_boards)
{
for (int i = 0; i < all_boards.Length; i++)
{
Response.Write(all_boards[i] + "<br />");
}
}
class board_me32
{
public string board_no { get; set; }
public string sl_no { get; set; }
public string Fr_no { get; set; }
}
}
а в результате выводится:
System.Collections.Generic.List`1[_class+board_me32]
...................................................
System.Collections.Generic.List`1[_class+board_me32]
Здравствуйте, kudzu, Вы писали:
K>что я делаю не так? подскажите
А почему ты считаешь, что ты что-то делаешь не так? Какой результат хочешь получить?
Re[2]: C# System.Collections.Generic.List`1
От:
Аноним
Дата:
11.05.10 11:46
Оценка:
Здравствуйте, Lloyd, Вы писали:
L>Здравствуйте, kudzu, Вы писали:
K>>что я делаю не так? подскажите
L>А почему ты считаешь, что ты что-то делаешь не так? Какой результат хочешь получить?
Хочу увидеть что-то такое:
board no=9 slot=2 frame no=1
.............................
board no=8 slot=2 frame no=2
board no=5 slot=1 frame no=1
Здравствуйте, Аноним, Вы писали:
L>>А почему ты считаешь, что ты что-то делаешь не так? Какой результат хочешь получить?
А>Хочу увидеть что-то такое: А>board no=9 slot=2 frame no=1 А>............................. А>board no=8 slot=2 frame no=2 А>board no=5 slot=1 frame no=1
Здравствуйте, Lloyd, Вы писали:
L>Здравствуйте, Аноним, Вы писали:
L>>>А почему ты считаешь, что ты что-то делаешь не так? Какой результат хочешь получить?
А>>Хочу увидеть что-то такое: А>>board no=9 slot=2 frame no=1 А>>............................. А>>board no=8 slot=2 frame no=2 А>>board no=5 slot=1 frame no=1
L>Для начала замени ToList на ToArray