0

I've developed a dll that allow me to use some method of my API. All working pretty well, but now I have a problem with linq. In particular I usually store all the results returned from the api in a list of object. So I can iterate through of it and get each item separated. Now I have this class:

public class Agent
{
        public int id { get; set; }
        public string name { get; set; }
}

public class RootObject
{
        public List<Agent> agent { get; set; } 
}

I deserialize the json like this:

var obj = JsonConvert.DeserializeObject<RootObject>(responseText);
return obj.Select(o => o.agent).ToList();

now I can deserialize the json correctly 'cause is a list of Agent but I can't use the method to return a list of object:

return obj.Select(o => o.agent).ToList();

the .Select is underlined in red, and the compiler tell me:

Agent.RootObject does not contain a definition for Select

if instead I use: var obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);

all the return line is underlined in red:

Can't convert System.Collections.Generic.List in System.Collections.Generic.List

so how can I fix this problem?

3
  • If obj is a single RootObject then you'd want obj.agent.Select(o => o.company), although I don't see company anywhere in your class definitions, Commented Apr 18, 2016 at 14:44
  • sorry fix typo. If you look at the agent class definition you can see that is a list Commented Apr 18, 2016 at 14:46
  • @IlDrugo In that case just use obj.Agent. Commented Apr 18, 2016 at 14:48

1 Answer 1

3

Select is an IEnumerable extension method, you cannot Select from obj unless it implements IEnumerable. In your case, you probably just need return obj.agent;.

Your 2nd attempt is showing an error on the whole line because your return type is most likely wrong. We would need to see your entire method, especially the signature, to tell you the exactly issue.

Sign up to request clarification or add additional context in comments.

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.