Trouble usinq Linq with SftpItem collection

0 votes
asked Oct 4, 2013 by Rebex KB (8,210 points)
edited Oct 4, 2013

I’m having trouble upgrading to build 2012R3. I have existing code that uses the ‘Where’ Linq operator with the Rebex.Net.SftpItemCollection in the Rebex.Sftp assembly. In the 2012R3 version Visual Studio 2010 appears to not recognize SftpItemCollection as implementing IEnumerable<sftpitem> although the object browser indicates that it does. All I’ve done is switch out the prior assemblies with the 2012R3 ones so I’m not sure what is going wrong. Any ideas?

This is an example of code which is not compilable after the upgrade:

Rebex.Net.SftpItemCollection sftpItemCollection = sftp.GetList(path);

foreach (var item in sftpItemCollection.Where(i => i.IsFile == true))
{
   Console.WriteLine(item.Name);
}
Applies to: Rebex SFTP

1 Answer

0 votes
answered Oct 4, 2013 by Tomas Knopp (58,890 points)
edited Oct 4, 2013
 
Best answer

This should be easily resolved by using

Cast<SftpItem>()

method as in the code below:

Rebex.Net.SftpItemCollection sftpItemCollection = sftp.GetList(path);
foreach (var item in sftpItemCollection.Cast<SftpItem>().Where(i => i.IsFile == true))
{
    Console.WriteLine(item.Name);
}

Sorry for inconvenience! This is because SftpItemCollection now implements both

IEnumerable<SftpItem>  and
IEnumerable<FileSystemItem>

(the latter one being part of the common IFtp API), so casting SftpItemCollection explicitly to IEnumerable<sftpitem> might be necessary in order to use the proper LINQ extension methods.

...