0

What is the proper syntax for this:

Dim qry As <??> = From f In dirInfo.GetFiles("*.QBW") Select File = f.FullName, Include = True
Dim dt As DataTable = qry.CopyToDataTable()

I tried as "IEnumerable(Of DataRow)" but that didn't work. At runtime it said:

Unable to cast object of type 'WhereSelectArrayIterator2[System.IO.FileInfo,VB$AnonymousType_02[System.String,System.Boolean]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.

Kevek
  • 2,534
  • 5
  • 18
  • 29
Denis
  • 11,796
  • 16
  • 88
  • 150

5 Answers5

2

Here is a small solution written in c#

Use select(x => new {})... and type it as IEnumerable

private static void Main(string[] args)
{
    var dirInfo = new DirectoryInfo(@"c:\windows\temp\");
    IEnumerable<dynamic> qry = dirInfo.GetFiles("*.txt").Select(x => new { x.FullName, x.Extension });

    foreach (var item in qry)
    {
        Console.WriteLine(string.Concat(item.Extension, " -> ", item.FullName));
    }
    Console.ReadKey();
}

Edit, here's a vb version

Private Shared Sub Main(args As String())
    Dim dirInfo = New DirectoryInfo("c:\windows\temp\")
    Dim qry As IEnumerable(Of dynamic) = dirInfo.GetFiles("*.txt").[Select](Function(x) New From { _
        x.FullName, _
        x.Extension _
    })

    For Each item As var In qry
        Console.WriteLine(String.Concat(item.Extension, " -> ", item.FullName))
    Next
    Console.ReadKey()
End Sub

Update 2

Now as clearly stated in the question the typing is unclear, this can be solved by using IEnumerable. However this is not an optimal solution, instead I recommend to build an object and store your data there. But for minor applications I suppose it'll do.

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • -1 for posting a C# answer to a VB.NET question. Also, using type inference (by using the `var` keyword) evades the actual question asked. Third, it seems to me (but I'm not 100% certain) that you've changed the meaning of the code with your usage of `new { … }`. – stakx - no longer contributing Sep 13 '11 at 19:02
  • 1
    Well it was flagged with c# when I wrote my answer thus i figured it was alright to type it in c#... However i've updated the post with a vb.net version so everyone will be happy as a rainbow. Using the Var keyword is recommended since .net4, what rock are you living under? The meaning of the post was to select several certain columns by using linq and the way Ive done it is the only way i know of. Oh please show us how you'd like to do it before criticizing. – Eric Herlitz Sep 13 '11 at 19:14
  • 1
    This was posted as a cross in C#/VB.NET because I didn't care which language the answer would be in. Was looking for the idea. Not fair for marking a C# answer with -1. @Trikks, Thank you for changing it to VB.NET, although unnecessary. – Denis Sep 13 '11 at 19:30
  • Downvote removed... sorry for that. However, the other two points still stand. Type inference just evades the question, but doesn't truly answer it. – stakx - no longer contributing Sep 13 '11 at 21:01
  • True, I've updated the post with some clarifications. Cheers :) – Eric Herlitz Sep 13 '11 at 22:26
2

I try to apply Linq in most situations so I am more familiar with it but this doesn't seem like the right problem to use it on. So going back to the old and true way... Per Everyone's help, I am arriving that the best way to do this would be:

  Dim dt As New DataTable()
  dt.Columns.Add("File", GetType(String))
  dt.Columns.Add("Include", GetType(Boolean))
  For Each f as FileInfo In dirInfo.GetFiles("*.QBW")
    dt.Rows.Add(f.FullName, True)
  Next
Denis
  • 11,796
  • 16
  • 88
  • 150
1
Dim qry = dirInfo.GetFiles("*.QBW").[Select](Function(f) New With { _
    f.FullName, _
    .Include = True _
})
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

Or simply turn Option Infer On, and don't use anything, like

Dim qry = From f In dirInfo.GetFiles("*.QBW") Select File = f.FullName, Include = True

Take a look here for a more in depth discussion.

However, to use CopyToDataTable() the type needs to a DataRow derived type.
What do you intend to use the DataTable for, there are lots of places in .net where a DataTable is not required?

Community
  • 1
  • 1
SWeko
  • 30,434
  • 10
  • 71
  • 106
  • What is the proper type for qry? Because I can't seem to do qry.CopyToDataTable()... – Denis Sep 13 '11 at 18:52
  • 1
    It's an anonymous type. The compiler creates and maintains it, and gives it a system generated name (the WhereSelectArray...]] part). – SWeko Sep 13 '11 at 18:54
-1

The solution is in the error message you pasted... you want an IEnumerable(Of FileInfo).

qJake
  • 16,821
  • 17
  • 83
  • 135
  • This is incorrect. The qry is not `Of FileInfo`, there is an anonymous type involved. – Anthony Pegram Sep 13 '11 at 18:45
  • The error message definitely does *not* say that. The `Select` is creating new instances of an anoymous type. – dlev Sep 13 '11 at 18:46
  • I would have assumed that it would be an IEnumerable(Of DataRow) where each DataRow contains 2 columns: "File" and "Include". How can I get to that? – Denis Sep 13 '11 at 18:48
  • Good, I was just thinking the same - how would I get FileInfo if I am selecting just a String and Boolean. – Denis Sep 13 '11 at 18:49
  • 1
    @Denis, `DataRow` is not involved in the query, it would never be `Of DataRow`. For this particular problem, if you want a `DataTable`, you would create it manually. Create the table structure, then loop over the query result and add new rows. – Anthony Pegram Sep 13 '11 at 18:50
  • @Anthony, I see. I was hoping I can do it through Linq somehow – Denis Sep 13 '11 at 18:56