❔ [RESOLVED] Find all Occurrences in All Files in Folder Issue
I have this code that works, but it only returns one result per file if string exists in file.
And, I know that the "FirstOrDefault" is why I only get 1 returned result from one particular file.
This particular file has 22 lines that meet the find criteria>
Here is my issue.
I have Tried "All, and Any" and get error because they are bool conditions. Here is my line..
This code works but I need to know what else can I use instead of "FirstOrDefault" to return All my results that match my search criteria.
string occurrence = lines.FirstOrDefault(l => l.Contains("DateTime.Now"));
Any input on this small issue will be appreciated a lot.
78 Replies
Where()?
What do you mean where? I just explained where. Please read again carefully. Thanks
no i meant the extension method Where()
use Where instead of FirstOrDefault
Doesn't like Where either.
read the error?
Yeah, but I don't know how to fix that.
occurence is a string
you want a collection of all lines that contain the string
it's giving you that
you can't assign that to a variable defined as a single string
Okay... I am getting all the strings in the for loop.
Then I guess I am not fetcing the collection properly in this loop.
Any advice code wise?
no, somehow you're printing what you intended to write as code as a string
or is that actually part of the text
That is this line shown in a messagbox....
string[] lines = File.ReadAllLines(file);
so what part isn't working?
This is the entire contents of the file
As I stated in my original post, its this line... (except I am trying "Where" now)
string occurrence = lines.Where(lines.Contains("DateTime.Now"));
... I just want to return every line where my search term matches.
you can join the list for example 🤔
so read what i said the first time
Where
does not return a string
it returns all strings that matchI actually used Join to display the MessageBox for debugging quickly.
Enumerable.Where Method (System.Linq)
Filters a sequence of values based on a predicate.
Yeah, I know. I am getting a boolean error. So, what is the fix??
what boolean error?
Yeah, I read that, not very helpful in my situation.
it is if you're trying to use
Where
See above. I posted a screenshot
your screenshot doesn't match the most recent snippet of code you shared, which one is current?
and i'm talking about the error in your screenshot
that's not a "boolean error"
clearly boolean error
it's a compiler error
Thats the issue... WHAT am i supposed to be using???
i can't tell you that... it's your problem you're trying to solve
read the error, it tells you what type Where is returning
and the example i linked shows you what you can do with that
Then why did I even come here. I thought this is where we get help?
it is
we help you learn to program, we don't fix it for you
Well, no disrespect, but you don't sound like helping
and all you've done is post a screenshot and say "it doesn't work"
Umm,, you didn't see all the code I implemented and tried. Why not help me "LEARN" something new here instead of being so hard-nosed.
i can help once you take my initial advice
Which one?
i linked you the documentation for
Where
that shows you what you should be doing with it
apply that to fix your error
this is the example on the pageYeah, I am looking at that.
Here is the current error, but if its all wrong, then it doesn't matter
i know what the current error is, and i'm telling you what you need to know to fix it
if you don't try to fix it, idk where to go from there
I am going to try, but I will have to re-write some portions. Its different than what I tried. Thanks
the hint is that
string
is not IEnumerable<string>
Yeah, I got that 🙂
Okay... updated code.
I get an error if I use "lines" as my collection.
The error is on image
But, If I use join I get no errors... Yeha. But, I get no results at all either.
you don't need to make a
List
, you already have a collection that you're calling .Where
onWasn't sure so, I went by the code.
I commented that line where I re-make the list and just edit this line like this...
IEnumerable<string> query = lines.Where(lines => lines.Length > 0);
i mean, you still need to use the search condition you actually want to use
However, when I run it, my app blinks and freezes. I assume its looping way to much or I don't have the code right
Okay, I know I already have a list, so I guess I don't know what you mean.
I am new to C# and I have never tried this before. So, please be patient with this 61 year old man.
I had a stroke a few years ago and suffered some brain damage, so I learn slower.
I tried this line, but get same error.
So, my current struggle is. I have a list from a file.....
string[] lines = File.ReadAllLines(file);
But, I can't seem to figure out how to convert so I can use it with the IEnumarable Where method that was suggested to me.
1) How do I link my "lines" to this List to get rid of error.
Basically how do I convert "lines" to work as a Collection String
List<string> txtStr = lines; <<--- ERROR
2) Where do I use my search term?
I had to discard it because the entire Where line above was broke and didn't work in my last code.
DateTime.Now
Thanks for any input.
....
Did some research... is this correct for converting???
I get no errors.... But, of course does return any results because I don't have my search term in there anymore. Just need to figure that out.
Not sure why I get this errore. I says Where needs 2 arguments. I have two. Obviously not the right ones. Any help would be appreciated.
What are you trying to query?
Not reading the whole thread so please summarise it here
A file as dicussed above
What's the query?
I am searching through every file in a folder with a specific search term.
I have the list of all text in each file.
But I am stuck on how to filter just the search term and list them the way they are in the file on each line.... and output to a TextBox
I did try this but was told it wouldn't work.
//string occurrence = lines.Where(l => l.Contains("DateTime.Now"));
I will try again with your suggestion.
What's
DateTime.Now
doing here?
Aren't you supposed to give a file name?
If you need a file creation date, you need an instance of System.IO.File
Oh alright this is supposed to be a C# code fileIts just from a file that stores all kinds of code I may use in the future.
My app is just a place to store code snippets.
What is this useless check?
Also use proper string interpolation
This is my first ever c# app.
This is older code.
I will post the lastest
Your string should look like this instead. You're setting Text property 3 times and wasting resources
It's okay
Lastest code:
Looks the same to me
Yeah it is. Sorry I had a stroke a few years back and have brain damage, so my attention is problematic and slow.
Well there's no need to say that
Well, I am just letting you know why I thought the code you posted was old.
It's alright
If this is your first time making a C# app, then I'm impressed
I did basically do that. Except the "CS" string part
Yeah.
cs
provides syntax highlightingOkay, didn't know. Cool.
I added CS to the post... looks nice
Also, I added your code suggestion. I get no errors, but I get no results either and my App freeze so that I have to "End Tasks"
It has to do with the foreach loop
Yeah, figure that. But, all my code was working before I posted for help. But the old code only returedn one line per file that had matching term. I want all lines in each file that matches term searched for.
I believe
Path.GetFileName
is the culprit
You're calling it dozens of times
I updated thisGot it working. Thanks @coroys
This is my final results. I also tweaked how the dashed lines appeared.
THANKS TO EVERYONE who shared their input with me. I do really appreciate it
Here is my final TEST code.....
After some refinement and adapting the test into my app. This is now complete. A simple Code Snippets program that uses search of files in a folder. I could of used a database, but I want to try the flat file search instead. This completes my first C# project.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.