LINQ结果集传递给其他方法

时间:2016-02-20 10:11:20

标签: c# linq

var result = datatable.AsEnumerable()
                  .GroupBy(row=>row.Field<DateTime>("Date"))
                  .Select(gr=>new 
                  {
                      Date = gr.Key,
                      Agents = gr.Select(x => new 
                      {
                          Id = x.Field<int>("ID"),
                          Agent = x.Field<string>("Agent")
                      })
                  });

    foreach (var res in result) {  
            var lstAgents= res.Agents;
    ImportSchedule(lstAgents);
}

如何在lstAgents方法中传递ImportSchedule()。我在ImportSchedule中定义了什么类型?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

public void ImportSchedule(dynamic agents) { }

但是您应该为此创建一个类,原因是如果属性Id更改为AgentId,那么您将在运行时发现错误。

欲了解更多信息:

Link 1

答案 1 :(得分:0)

您无法将匿名类型传递给方法,但您可以像这样创建类或结构:

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("davids-macbook-pro.local")
                .setPort(5222)
                .setDebuggerEnabled(true) // to view what's happening in detail
                .build();

AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
    conn2.connect(); // move it to a background thread instead of main thread
    // conn2.login();
} catch (SmackException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (XMPPException e) {
    e.printStackTrace();
}


conn2.addConnectionListener(new ConnectionListener() {
    @Override
    public void connected(XMPPConnection connection) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    conn2.login();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {

    }

    @Override
    public void connectionClosed() {

    }

    @Override
    public void connectionClosedOnError(Exception e) {

    }

    @Override
    public void reconnectionSuccessful() {

    }

    @Override
    public void reconnectingIn(int seconds) {

    }

    @Override
    public void reconnectionFailed(Exception e) {

    }
});

在这种情况下,您可以像这样使用make方法ImportSchedule:

public class AgentDto
{
    public int Id {get; set;}
    public string Agent {get; set;}
}

另请注意,您没有将日期传递给ImportSchedule,但您可能需要这样做