Reuel
Reuel

Reputation: 31

Error: System.Collections.Generic.List<AnonymousType#1>

I'm getting the error. Here's my function:

public List<PlainBrgMetric> GetPlainBrgMetricProgram(long programLOBID) 
{
    var query = _context.metrics.Join(_context.universals,
                                      m => m.metricID,
                                      u => u.orderByAsc,
                                     (metric, universal) => new 
                                     {
                                         metric.metricID,
                                         metric.programLOBID,
                                         metric.label,
                                         universal.groupValue1
                                     }).ToList();
    return query;
}

Upvotes: 0

Views: 62

Answers (2)

Christos
Christos

Reputation: 53958

This is the expected behavior, because here:

(metric, universal) => new 
                       {
                           metric.metricID,
                           metric.programLOBID,
                           metric.label,
                           universal.groupValue1
                       }

you create an anonymous type and not a PlainBrgMetric object.

Provided that PlainBrgMetric has at least the same four properties as the anonymou's type properties, you create, you could make a quick fix:

(metric, universal) => new PlainBrgMetric 
                       {
                           MetricID = metric.metricID,
                           ProgramLOBID = metric.programLOBID,
                           Label = metric.label,
                           GroupValue1 = universal.groupValue1
                       }

Otherwise you have to declare another type with these four properties and change both the signature of your method and the type you create above for each result of the join.

I didn't mention the alternative of the dynamic object, since I assumed from your code that you want to return a collection of strongly typed objects.

Upvotes: 1

user449689
user449689

Reputation: 3154

To fix it you must return a list of PlainBrgMetric, what you are returning is a list of anonymous objects.

You should edit your code as follows:

public List<PlainBrgMetric> GetPlainBrgMetricProgram(long programLOBID) 
{
    var query = _context.metrics.Join(_context.universals,
                                  m => m.metricID,
                                  u => u.orderByAsc,
                                 (metric, universal) => new PlainBrgMetric
                                 {
                                     //Populate the object properties
                                     ...
                                 }).ToList();
    return query;
}

Upvotes: 1

Related Questions