Convert anonymous type to array or arraylist. It can be done

Trying to extract data from linq from a database. I would like to use anonymous types and convert to Ilist, Array, ArrayList or Collection. Data is used in a third-party object that accepts an Ilist, arraylist, or collection.

I can't get this to work. I get the following error: "Sequence statements are not supported for type" System.String "

using (var db = new dbDataContext())
{
    var query = from e in db.people
                select new
                {
                    Count = e.LastName.Count()
                };

    Array test;
    test = query.ToArray();
}
+3
source share
2 answers

This has nothing to do with converting the results to lists of arrays or even anonymous types. Here is another version of your code that will fail:

using (var db = new dbDataContext())
{
    var query = db.people.Select(x => x.LastName.Count());
    foreach (int x in query)
    {
        Console.WriteLine(x);
    }
}

- , :

x => x.LastName.Count()

SQL, .

:

x => x.LastName.Length

, , . , # - LINQ to SQL.

, - . , , , , , , - .

+8

ArrayList , ICollection.
List LINQ.

using (var db = new dbDataContext()) {
        var query = from e in db.people
                    select new { Count = e.LastName.Count() };

        ArrayList list = new ArrayList(query.ToList());
}

Visual Studio ( Mac), . (ToArray )

, Count() .

+2

Source: https://habr.com/ru/post/1730922/


All Articles