Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am generating multiple CSV's in a folder ,I have to merge all of them and make one file.
P.s.
1.They all will have same headers.
2.Their names are not fixed and will be changed every other day acc to date and some other parameters.
Not really tested but should give you an idea:
var allCsv = Directory.EnumerateFiles("Src-Path", ".*csv", SearchOption.TopDirectoryOnly);
string[] header = { File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l)) };
var mergedData = allCsv
.SelectMany(csv => File.ReadLines(csv)
.SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1)); // skip header of each file
File.WriteAllLines("Dest-Path", header.Concat(mergedData));
Note that you have to add using System.Linq;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
So I made a simple c# file reader because I was bored and added an if statement to filter the results. But when I ran it, it gave me more results than I wanted. I was supposed to get
276, 2, and there was only one line inside the file with that value, but I got multiple. I checked the file and it had lines ending with the same value. I tried string.Equals(line, "276, 2") but it gave me the same results. I doubt there isn't something in c# that doesn't solve this issue.
You can use Regex, as it's mentioned in this issue
bool result = Regex.IsMatch(line, "\\b276, 2\\b");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to clear just birkatmankontur list but this code clears all lists in my program
katmankonturlari.Add(ii, birkatmankontur);
katmankonturlari.ToList();
birkatmankontur.Clear();
You probably want something like this:
// Create a shallow copy of birkatmankontur and add it into katmankonturlari
katmankonturlari.Add(ii, birkatmankontur.ToList());
// Clear original birkatmankontur;
// note, that katmankonturlari contains birkatmankontur's copy
birkatmankontur.Clear();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm reading an email and performing a few operations.
In email it can't be in defined format as below,
Email formats:
Hi Team,
Please create 7025-45-365-14, 9851-98-524-12
5741-55-452-45
Thanks
MailItem.Body = "Hi Team,\r\n\r\n \r\n\r\nPlease create 7025-45-365-14, 9851-98-524-12\r\n\r\n5741-55-452-45.\r\n\r\n \r\n\r\nThanks\r\n"
My goal is to exact only the string parts into a loop one by one as below,
1st loop > 7025-45-365-14
2nd loop > 9851-98-524-12
3rd loop > 5741-55-452-45
I tried various logic but I can't extract as I need. Can someone help me?
To extract digits in that pattern (4-2-3-2)
foreach (var m in new Regex(#"\b(\d{4}-\d{2}-\d{3}-\d{2})\b").Matches(mail))
Console.WriteLine(m.Value);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I get a count of all items in a Mongo collection, using the 2.x C# driver?
I'm trying to use CountAsync, which I need to pass in a filter. I don't want to filter - I want everything returned.
You can always pass the empty document in the method like this
db.collection.CountDocumentsAsync(new BsonDocument());
db.collection.count()
In C# you can use Count() on the cursor of your collection.
Try as below:
var mongo = new Mongo();
mongo.Connect();
var db = mongo.GetDatabase("DatabaseName");
var collection = db.GetCollection<Product>();
var totalCount= collection.Count();
you can check in the following URL
this url
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have 2 text file in C# say: File A & File B. I want to compare both file contents and if there is any content found in File A which is present in File B then i want to remove that content in File B .
So if is there any method in c# which can do this for me then please let me know?
If you want to compare the lines you could use this query:
var newLines = File.ReadLines(f2Path).Except(File.ReadLines(f1Path)).ToList();
File.WriteAllLines(f2Path, newLines);
Edit: according to your comment(s)
i want to exclude all lines from file2 where the id exists in file1
You could use this left-outer-join with LINQ:
var idInF2 = from f2Line in File.ReadLines(f2Path)
join f1Line in File.ReadLines(f1Path)
on f2Line.Split(',')[0].Trim() equals f1Line.Split(',')[0].Trim() into gj
from lineOne in gj.DefaultIfEmpty()
where lineOne == null
select f2Line;
File.WriteAllLines(f2Path, idInF2.ToList());
or you could use this more readable but less efficient approach using Contains:
var f1IDs = File.ReadLines(f1Path).Select(l => l.Split(',')[0].Trim());
var newLines = File.ReadLines(f2Path)
.Select(l => new { Line = l, ID = l.Split(',')[0].Trim() })
.Where(x => !f1IDs.Contains(x.ID))
.Select(x => x.Line).ToList();
File.WriteAllLines(f2Path, newLines);