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);
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 2 years ago.
Improve this question
I have something like this:
var test = teamMemberInfoList
.Select(p => new {
p.AssignedTo,
p.RemainingCapacity,
p.RemainingWork,
p.RemainingWork-
p.RemainingCapacity })
.ToArray();
I would like a list of arrays of strings from values from x numbers of properties. Is there anyway to do this? And the C# line does not work because I cannot do plus or minus either that is also a wish.
Yes you could use Tuple
test will be an array of Tupe>string>
var test = teamMemberInfoList.Select(p => (p.AssignedTo, p.RemainingCapacity,p.RemainingWork, p.RemainingWork - p.RemainingCapacity )).ToArray();
or List and test will be an array of List
var test = teamMemberInfoList.Select(p => new List<string>(){p.AssignedTo, p.RemainingCapacity,p.RemainingWork, p.RemainingWork - p.RemainingCapacity }).ToArray();
You can do plus or minus but you need to provide some name for resulting property (for others compiler will reuse the member access ones):
var test = teamMemberInfoList
.Select(p => new
{
p.AssignedTo,
p.RemainingCapacity,
p.RemainingWork,
Diff = p.RemainingWork- p.RemainingCapacity
})
.ToArray();
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;
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
Ok I got the result in client textarea depend on user choose how many ip addresses they want like this,
Please add the following DNS entries
165.216.237.48 name.domain
165.216.237.250 name.domain
The number of entries can vary, it could list 1, 3, or 5 ip addresses with domain name. I want C# to get the ip address and name.domain in every new line then split and store in 2 separate arrays for ip and domain. The C# textarea code is txt.Text. The first line need to be ignored. Advice?
var ips = new List<string>();
var domains = new List<string>();
foreach (var elements in txt.Text
.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(line => line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)))
{
ips.Add(elements[0]);
domains.Add(elements[1]);
}
ipsArray = ips.ToArray();
domainsArray = domains.ToArray();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Currently, I have a List<object> like this: List<USER>.
In User class, I have column named UserName.
I want to retrieve this column to List<string>.
How can it work without using foreach for List<USER>?
Thanks.
You can use LINQ:
List<string> userNames = users.Select(u => u.UserName).ToList();
In addition to Tim Schmelter post:
For casting IEnumerable<object> to IEnumerable<USER> you may use Cast<T> or OfType<T> method.
This can be written as follows:
var userNames = users.Cast<USER>.Select(u => u.UserName).ToList();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I don't have much code to show but I am trying to remove a substring (individual number) from a longer string is a series of numbers.
Ex: Main String is "11,12,15,16,55,33,88,100,121,155,115"
Need to find number 16 and remove it from the string leaving...
11,12,15,55,33,88,100,121,155,115
They are a list of id's from a database so I can't just change them to strings. Also how do I remove it as if it wasn't there?
string numbers = "11,12,15,16,55,33,88,100,121,155,115";
numbers = string.Join(",", numbers.Split(',').Where(num => num != "16"));
But why don't you use a List<int> instead for database ID's?
In this specific use case I would split the string using "," as the separator, remove the element that matches, then join the elements again.