a simple linq query

T

Tony Johansson

Hello!

I have this simple linq query
var test = from o in answer
group o by o.Value into grp
select new { count = grp.Count() };

I want to put the test into an array something like
string[] myArray = test.ToArray();
or into a List

If I try this string[] myArray = test.ToArray();
I get the following compile error
Cannot implicitly convert type 'AnonymousType#1[]' to string[]'

How to I do to store my test result into a variable of type string[] or
List?

//Tony
 
A

Anthony Jones

Tony Johansson said:
Hello!

I have this simple linq query
var test = from o in answer
group o by o.Value into grp
select new { count = grp.Count() };

I want to put the test into an array something like
string[] myArray = test.ToArray();
or into a List

If I try this string[] myArray = test.ToArray();
I get the following compile error
Cannot implicitly convert type 'AnonymousType#1[]' to string[]'

How to I do to store my test result into a variable of type string[] or
List?

var test = from o in answer
group o by o.Value into grp
select grp.Count().ToString();

Now that the LINQ creates an IEnumerable<String> you can use ToArray to
create a string[]
 
M

Michael C

Tony Johansson said:
Hello!

I have this simple linq query
var test = from o in answer
group o by o.Value into grp
select new { count = grp.Count() };

The new { } part is creating an anonymous type which will be a class with 1
property call count
I want to put the test into an array something like
string[] myArray = test.ToArray();
or into a List

If I try this string[] myArray = test.ToArray();
I get the following compile error
Cannot implicitly convert type 'AnonymousType#1[]' to string[]'

How to I do to store my test result into a variable of type string[] or
List?

replace your select statement with this:
select grp.Count().ToString()

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top