How do I return an array from a function from a LINQ anonymous list?

Basically, I want to have a generic function that takes a LINQ anonymous list and returns an array. I was hoping to use generics, but I just can get it to work.

hope the below example helps

Let's say I have a person object with id, fname, lname and dob. I have a generic class that contains a list of objects.

i returns an array of faces back

my code snippet would be something like

dim v = from p in persons.. select p.fname,p.lname

      

i is now anonymous type from system.collections.generic.ineumerable (of t)

to bind this to the grid I would have to iterate over and add to the array eg.

dim ar() as array

for each x in v
    ar.add(x)
next

grid.datasource = ar

      

I don't want to constantly iterate as I might have different objects

I need a function that does something like below:

function getArrayList(of T)(dim x as T) as array()

    dim ar() as array

    for each x in t
       ar.add(x)
    next

    return ar
end

      

hope this clears up. how can I get a generic function with taking an anonymous list ienumearable and returning the array back. unfortunately the one I have is not working.

Thanks a lot in advance as all pointers / help will be INCLUDED.

considers

Azad

0


source to share


4 answers


You can bind the grid directly to an array of anonymous types. Here's an example:

        var qry = from a in Enumerable.Range(0, 100)
                  select new { SomeField1 = a, SomeField2 = a * 2, SomeField3 = a * 3 };
        object[] objs = qry.ToArray();
        dataGridView1.DataSource = objs;

      



Notice also the call to ToArray, which removes the need for a loop. I also assign it to the object type [] to demonstrate that you can pass it as your type if you like.

+3


source


You just call ToArray . Of course the type is anonymous ... but because of type inference, you don't need to specify the type name.

From the example code:



    packages _
    .Select(Function(pkg) pkg.Company) _
    .ToArray()

      

The company turns out to be a string, but there is no reason why it cannot be anything.

+2


source


Your question is a bit unclear, so I'm not sure how much my answers will help, but here goes ...

  • An anonymous type has a method scope , so you cannot return it from a function, at least not in a strongly typed form. You can use object

    and then re-create your anonymous type
    , but you'd be better off declaring a simple class with some automatic properties .
  • To convert IEnumerable

    to an array, just callToArray()

  • However, you can DataBind

    directly in IEnumerable

    , no need to convert it to an array
  • If the data you are dealing with may have different fields, you might be better off creating DataTable

    and binding it to your DataGrid.
+1


source


I'm not sure if you can easily pass anonymous objects as parameters, more than you can get as return values.

I say it easily because:

[Try the code formatting option in your question (the button in the editor), this will make it easier to read the parts of your question that are code snippets.]

0


source







All Articles