Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that should return only one filename, and this is based on whether the name of the file contains a certain string. I am able to enumerate files from the directory but i am getting
System.Linq.Enumerable+WhereSelectEnumerableIterat
for the select instead of the actual filename. In the folder i have files named :

GL_Export_54_201908220333.xml
GL_Export_55_201907081053.xml
GL_Export_56_201908220337.xml
GL_Export_78_201907081055.xml

Code :
C#
<pre>string period = "55";
 var batchperiod = period.ToString();
 string BatchInPath = @"C:\Users\myuser\source\repos\MySolution\MyProject\BatchIn";
 IEnumerable<string> fileList = Directory.EnumerateFiles( BatchInPath);
  var CurrentBatchName = from file in fileList
                         let fileName = Path.GetFileName(file)
                         where fileName.Contains(batchperiod)
                         select fileName.ToString().First();            
Console.WriteLine(CurrentBatchName)


So with batchperiod as above:

EXPECTED OUTPUT

C#
GL_Export_55_201907081053.xml



ACTUAL OUTPUT
C#
System.Linq.Enumerable+WhereSelectEnumerableIterat


What I have tried:

C#
<pre> string period = "55";
 var batchperiod = period.ToString();
 string BatchInPath = @"C:\Users\myuser\source\repos\MySolution\MyProject\BatchIn";
 IEnumerable<string> fileList = Directory.EnumerateFiles( BatchInPath);
  var CurrentBatchName = from file in fileList
                         let fileName = Path.GetFileName(file)
                         where fileName.Contains(batchperiod)
                         select fileName.SingleOrDefault();            
Console.WriteLine(CurrentBatchName)
Posted
Updated 10-Jan-20 0:33am
Comments
Maciej Los 10-Jan-20 7:50am    
Your requirement is not clear. Please, read all answers to find out why, especially the answer provided by @phil.o.

Try:
C#
string batchperiod = "_55_";
string BatchInPath = @"C:\Users\myuser\source\repos\MySolution\MyProject\BatchIn";
IEnumerable<string> fileList = Directory.EnumerateFiles(BatchInPath);
var CurrentBatchName = (from file in fileList
                       let fileName = Path.GetFileName(file)
                       where fileName.Contains(batchperiod)
                       select fileName).FirstOrDefault();
Console.WriteLine(CurrentBatchName);
 
Share this answer
 
Comments
Maciej Los 10-Jan-20 7:49am    
This is correct while OP wants to get only first "instance" of 55 ;)
phil.o 10-Jan-20 8:13am    
Adding prefix and suffix underscore is enough to discriminate in this case.
Just do something simple like

var CurrentBatchName = fileList.FirstOrDefault(f => f.Contains(batchperiod));
 
Share this answer
 
Comments
Maciej Los 10-Jan-20 7:47am    
5!
C#
string period = "55";
var batchperiod = period.ToString();
Why are you doing this? Calling ToString() on a variable which already is a string just does not make any sense.
Moreover, Enumerable.SingleOrDefault Method[^] throws an exception if there is more than one element in the sequence. Which is the case here, since these lines:
GL_Export_55_201907081053.xml
GL_Export_78_201907081055.xml
both contains the 55 character sequence.
Maybe you need a regular expression to filter the result more acurately?
 
Share this answer
 
Comments
Maciej Los 10-Jan-20 7:47am    
Good catch!
phil.o 10-Jan-20 8:12am    
Thanks Maciej :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900