Reputation: 3666
I'm trying to return an IEnumerable<string>
from a linq query and I'm getting the error:
Cannot convert IEnumerable<AnonymousType>
to IEnumerable<string>
I have no idea how to go about fixing this issue.
private IEnumerable<string> GetAllMembers(Type type)
{
var query =
(from member in type.GetMembers()
select new
{
Member = member.GetType() + " " + member.ToString()
}
);
return query;
}
Upvotes: 2
Views: 10194
Reputation: 14672
Try this
private IEnumerable<string> GetAllMembers(Type type)
{
var query =
from member in type.GetMembers()
select member.GetType() + " " + member.ToString();
return query;
}
You were creating an IEnumerable of anonymous types, when I think you just wanted an enumeration of strings.
Upvotes: 5
Reputation: 136114
The problem is your query returns an anonymous object with one property Members
which is a string, simply change to just returning the string:
var query =
(from member in type.GetMembers()
select member.GetType() + " " + member.ToString()
);
return query;
Upvotes: 2
Reputation: 16042
You are returning an anonymous type (constructed by new {}
) from your query and not a string.
You can modify the query like this for example, so the result is an IEnumerable<string>
:
return from member in type.GetMembers()
select member.GetType() + " " + member.ToString();
Upvotes: 2
Reputation: 236228
You are creating anonymous type with Member
property. Simply return string values thus you are expect IEnumerable<string>
private IEnumerable<string> GetAllMembers(Type type)
{
return from member in type.GetMembers()
select member.GetType() + " " + member.ToString();
}
Upvotes: 3