如何使用C#和Linq使c#方法更具功能性

net*_*x01 0 c# linq

private void DoSomeWork()
{
    ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount];

    int i = 0;

    foreach (DataRow row in StoredProcedureParameters.Tables[0].Rows)
    {
        waitEvents[i] = new ManualResetEvent(false);
        var workerClass = new WorkerClass();

        var collection = new Dictionary<string, object>();
        foreach (DataColumn dataColumn in StoredProcedureParameters.Tables[0].Columns)
        {
            collection[dataColumn.ColumnName] = row[dataColumn.ColumnName];
        }

        workerClass.Parameters = collection;
        workerClass.Event = waitEvents[i];
        i++;

        ThreadPool.QueueUserWorkItem(WorkerFunction, workerClass);
    }
}

private class WorkerClass
    {
        public ManualResetEvent Event;
        public Dictionary<string, object> Parameters;
    }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

好吧,一些改进:

private void DoSomeWork()
{
    DataTable table = StoredProcedureParameters.Tables[0];
    var columns = table.Columns.Select(x => x.ColumnName).ToList();

    var workers = table.Rows.Select(row => new WorkerClass
               (new ManualResetEvent(false), 
                columns.ToDictionary(column => row[column]));

    foreach (WorkerClass worker in workers)
    {
        worker.Start();
    }
}

...

// This is now immutable, and contains the work that needs to be done for the
// data it contains. (Only "shallow immutability" admittedly.)
class WorkerClass
{
    private readonly ManualResetEvent resetEvent;
    private readonly Dictionary<string, object> parameters;

    public WorkerClass(ManualResetEvent resetEvent, 
                       Dictionary<string, object> parameters)
    {
        this.resetEvent = resetEvent;
        this.parameters = parameters;
    }

    public void Start()
    {
        ThreadPool.QueueUserWorkItem(WorkerFunction);
    }

    private void WorkerFunction(object ignored)
    {
        // Whatever
    }
}
Run Code Online (Sandbox Code Playgroud)