1

I'm trying to convert a IList<IList<object>> to IList<object>, I mean to have a unique and one list where contains all the element (object) of the first one.

    public IList<IList<Page>> PMTs
    {
        get
        {
            var pmts = Processes.Select(x => x.PageMapTable)
                                .ToList();
            return pmts;
        }
    }

    public IList<Page> BlockMapTable
    {
        get
        {
            // Incomplete
            var btm = PMTs.Select(x => x.?? ..
        }
    }

You have a few problems. The first problem is that Split(). You need to change inputLine.Split() to inputLine.Split(','). Right now you are calling into the overload of System.String.Split(params char[]) and since you are not specifying any characters to split, it will return the entire string.

The other issues (as an CS student), you should really work on your naming conventions and error checking. The code is rather brittle and will break very easily. You should start early on learning good software engineering practices and writing high quality code.

using (FileStream fstream = new FileStream("path", FileMode.Open))
using (StreamReader reader = new StreamReader(fstream)) {
    string line;

    while (!reader.EndOfStream && (line = reader.ReadLine()) != null) {
        string[] data = line.Split(',');

        if (data.Length < 5) {
            // You will have IndexOutOfRange issues
            continue; // skip processing the current loop
        }

        int employeeNumber;
        string employeeName;
        string employeeAddress;
        double employeeWage;
        double employeeHours;

        // Will be used to check validity of fields that require parsing into a type.
        bool valid;

        valid = int.TryParse(data[0], out employeeNumber);

        if (!valid) {
            // employee number is not parsable
        }

        employeeName = data[1];
        employeeAddress = data[2];

        valid = double.TryParse(data[3], out employeeWage);

        if (!valid) {
            // employee wage is not parsable
        }

        valid = double.TryParse(data[4], out employeeHours);

        if (!valid) {
            // employee hours are not parsable
        }
    }
}
4

2 回答 2

4

假设您想要 flatMap/flatten list<list<Page>>,您可以使用 selectMany 方法来做到这一点,如下所示:

public IList<Page> BlockMapTable
{
    get
    {
        var btm = PMTs.SelectMany(x => x).ToList();
    }
}

如果您想了解更多信息,这里有一篇关于 selectMany 扩展方法的精彩博文

于 2012-04-15T06:17:12.930 回答
1
public IList<Page> BlockMapTable
    {
        get
        {
            return PMTs.SelectMany(p => p).ToList();
        }
    }
于 2012-04-15T06:17:17.447 回答