1

I am using below method to get list. Although there are records on 'lc' it doesn't pass to the list .

public void assignList(List<list_chart> lc, DataTable dtMetric, string metricname)
        {
            lc = (from dr1 in dtMetric.AsEnumerable()
                  where dr1.Field<string>("METRIC_NAME") == metricname
                  orderby dr1["SAMPLE_TIME"] ascending
                  select new list_chart
                  {
                      SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
                      Value = dr1.Field<decimal>("value")
                  }).ToList();
        }

//lc has 100 records but listchart1 still shows 0 after i call method.
assignList(listchart1, dtMetric1, ucMetrics.CheckedItems[0].DisplayText);

What do i miss?

S. Doe
  • 95
  • 8

3 Answers3

3

Since you are assigning lc to a new object that's why on caller invoker the reference is still pointing to old address of list. To solve this, either you should return the list back from assignList method or use the list by Reference.

Solution 1: (Return the list)

public List<list_chart> assignList(List<list_chart> lc, DataTable dtMetric, string metricname)
{
    lc = (from dr1 in dtMetric.AsEnumerable()
          where dr1.Field<string>("METRIC_NAME") == metricname
          orderby dr1["SAMPLE_TIME"] ascending
          select new list_chart
          {
              SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
              Value = dr1.Field<decimal>("value")
          }).ToList();

      return lc;
}

Solution 1.1 If you want to keep the record of lc intact then use below approach:

public void assignList(List<list_chart> lc, DataTable dtMetric, string metricname)
{
    var result = (from dr1 in dtMetric.AsEnumerable()
          where dr1.Field<string>("METRIC_NAME") == metricname
          orderby dr1["SAMPLE_TIME"] ascending
          select new list_chart
          {
              SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
              Value = dr1.Field<decimal>("value")
          }).ToList();

     lc.AddRange(result);

}

Solution 2: (List by Reference)

public void assignList(ref List<list_chart> lc, DataTable dtMetric, string metricname)
{
    lc = (from dr1 in dtMetric.AsEnumerable()
          where dr1.Field<string>("METRIC_NAME") == metricname
          orderby dr1["SAMPLE_TIME"] ascending
          select new list_chart
          {
              SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
              Value = dr1.Field<decimal>("value")
          }).ToList();
}

I would recommend you to use the Solution 1.

user1672994
  • 10,509
  • 1
  • 19
  • 32
2

If you just modified LC by adding items it would have worked. By now you are replacing. this means assignList just has a different reference (to a new created list.)

Just return lc or use ref keyword before lc.

public List<list_chart> assignList(DataTable dtMetric, string metricname)
        {
            lc = (from dr1 in dtMetric.AsEnumerable()
                  where dr1.Field<string>("METRIC_NAME") == metricname
                  orderby dr1["SAMPLE_TIME"] ascending
                  select new list_chart
                  {
                      SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
                      Value = dr1.Field<decimal>("value")
                  }).ToList();
            return lc;
        }

//lc has 100 records but listchart1 still shows 0 after i call method.
listchart1 = assignList(dtMetric1, ucMetrics.CheckedItems[0].DisplayText);
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

That's because you are creating a new list in the method.

In order to support that, you need to use the ref keyword:

public void assignList(ref List<list_chart> lc, DataTable dtMetric, string metricname)
{
    lc = (from dr1 in dtMetric.AsEnumerable()
          where dr1.Field<string>("METRIC_NAME") == metricname
          orderby dr1["SAMPLE_TIME"] ascending
          select new list_chart
          {
              SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
              Value = dr1.Field<decimal>("value")
          }).ToList();
}
//lc has 100 records but listchart1 still shows 0 after i call method.
assignList(ref listchart1, dtMetric1, ucMetrics.CheckedItems[0].DisplayText);

A better approach would be to return the List from the function.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99