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]'.

3
  • 1
    @Trikks, that would be an instance of System.IO.DirectoryInfo, given the usage. Commented Sep 13, 2011 at 18:51
  • @Anthony is right, dirInfo is "System.IO.DirectoryInfo" Commented Sep 13, 2011 at 18:55
  • Ah true that, missed the obvious. Should have been in the description though would have saved us some time! :) Commented Sep 13, 2011 at 18:57

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

-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 { … }.
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.
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.
Downvote removed... sorry for that. However, the other two points still stand. Type inference just evades the question, but doesn't truly answer it.
True, I've updated the post with some clarifications. Cheers :)
1
Dim qry = dirInfo.GetFiles("*.QBW").[Select](Function(f) New With { _
    f.FullName, _
    .Include = True _
})

3 Comments

but then I can't do qry.CopyToDataTable()? What type is "qry"?
qry is an IEnumerable(Of an anonymous type). Declare a new class containing the 2 properties FullName and Include if you need an explicit type.
You can't use CopyToDataTable in this case. See msdn.microsoft.com/en-us/library/5ycd1034(v=vs.80).aspx#Y662 for adding rows to a table
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?

2 Comments

What is the proper type for qry? Because I can't seem to do qry.CopyToDataTable()...
It's an anonymous type. The compiler creates and maintains it, and gives it a system generated name (the WhereSelectArray...]] part).
-1

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

6 Comments

This is incorrect. The qry is not Of FileInfo, there is an anonymous type involved.
The error message definitely does not say that. The Select is creating new instances of an anoymous type.
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?
Good, I was just thinking the same - how would I get FileInfo if I am selecting just a String and Boolean.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.