0

I have a method(method A) that returns an Ienumerable object.

The object is a datacontract class that has the following fields:

Id
TotalViews

I have another method(method B) that call calls Method A

Method A has the following:

List<string> response;
  
response = ExecuteRequest(uri).Select(d => new {d.Id, d.TotalViews }).ToList();

I trying to assign the ienumerable object to a list.

The above does not work as I get errors.

Error

cannot implicitly convert type system.collections.generic.list to system.collections.generic.list

How do I assign all the Ienumerable object to a list?

3 Answers 3

4

You can't - because the result isn't a list of strings.

You could convert it to a list of strings, e.g.

List<string> response = ExecuteRequest(uri)
                              .Select(d => new {d.Id, d.TotalViews }.ToString())
                              .ToList();

... but I doubt that's what you really want. You should ask yourself:

  • Why did you originally declare response to be of type List<string>?
  • What type do you really want?
  • If you want a pair of Id / TotalViews as per the anonymous type, there are three options:
    • If you only need it to be strongly-typed within the method, you can stick with an anonymous type, and declare the variable with var
    • Otherwise... you could build your own type with Id and TotalViews properties...
    • ... or you could potentially use Tuple<,> if you're using .NET 4

Here's the version using an anonymous type:

// The compiler knows the type of response, but you can't express it in normal
// C#, because it's IEnumerable<X> where X is the anonymous type... which you
// can't specify the name of, because it's anonymous...
var response = ExecuteRequest(uri).Select(d => new {d.Id, d.TotalViews })
                                  .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to stuff an anonymous type made from d.Id and d.TotalViews into a list of strings.

Either create a string from both of these before calling ToList on the result, or create a containing class for these and make a list of those instead of a String.

public class TotalViewsPerId
{
  public int Id {get;set;}
  public int TotalViews {get;set;}
}

List<TotalViewsPerId> response = ExecuteRequest(uri)
                              .Select(d => new TotalViewsPerId{d.Id, d.TotalViews })
                              .ToList();

Comments

1

You have declared a list of string, but you are trying to assign a list of an anonymous type. You could choose one of these options:

  • Change the declaration to match the type you are trying to assign (in this case, an anonymous type):

    var response = ExecuteRequest(uri).Select(d => new {d.Id, d.TotalViews }).ToList();
    

Note that you can't return an anonymous type from a method.

  • Change the type that you are trying to assign to match the declaration (in this case, List<string>):

    response = ExecuteRequest(uri).Select(d =>
            string.Format("{0} {1}", d.Id, d.TotalViews)
        ).ToList();
    
  • Change both. Maybe a Dictionary<int, int> would be better for your needs?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.