0
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);
}

how can i pass lstAgents in ImportSchedule() method. What type i define in ImportSchedule?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Devendra
  • 99
  • 2
  • 10
  • 1
    When you do 'Select(gr=>new ...' you are creating an anonymous type. Here you could find an answer to your question: http://stackoverflow.com/questions/6624811/how-to-pass-anonymous-types-as-parameters. – Jose Luis Feb 20 '16 at 10:19

2 Answers2

1

You can do it like this:

public void ImportSchedule(dynamic agents) { }

But you should make a class for this, the reason being if the property Id is changed to AgentId then you will find out the error on the Run time.

For more info:

Link 1

Community
  • 1
  • 1
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
0

You can't pass anonymous type into method, but you can make class or struct like this:

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

in this case you shuld make method ImportSchedule like this:

public void ImportSchedule(IEnumerable<AgentDto> agents)

also take your attention that you not pass the date into ImportSchedule, but you probably need to do it

gabba
  • 2,815
  • 2
  • 27
  • 48