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
I have the following data:
1,1,1,1,1,1
I want to read that line and covert it into array of ints or some other type.
Is there a one line solution to do that?
var arr = "1,1,1,1,1,1".Split(',').Select(s => int.Parse(s)).ToArray();
In case your text includes some spaces
var arr = Regex.Matches("1,1,1,1,1,1", #"\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
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
I have some strings, like below
a1
c1
b0
I want to sort them to a1b0c1
I tried the code below:
string answer1 = new string(answer.OrderBy(c => c).ToArray());
But the result is 011abc
How to make the string a1b0c1?
I assume, you have one string consisting of several lines. You have to split your string into an array, sort this and join the lines together:
string input = "a1\nc1\nb0";
string[] lines = input.Split('\n');
string result = string.Join("\n", lines.OrderBy(x => x));
Online demo: https://dotnetfiddle.net/u32Pk9
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 4 years ago.
Improve this question
I want to remove some string from regex in c#.
I have a string
000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip
From this I want to remove those strings
140702
142110
And I want a result as
000603ABC-005051-I-FILL200-NNYYNY180319--A2002.zip
How can I do that in regex?
You can use the Regex.Replace method (https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx).
var regex = new Regex("(140702)|(142110)");
var result = regex.Replace("000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip", "");
Although, as already stated by #CompuChip, String.Replace would probably be more efficient in this case. There's a discussion of various methods of replacing substrings and their efficiency at https://blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance/.
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 need a c# function that takes a string:
Input: "str1__value__xyz__str4__"
I want to convert it to an array (or list) of string
Output: string[] output = { str1, value, xyz, str4}
I think we can use Linq or a regular expression.
Input.Split(new string[] { "__" }, StringSplitOptions.None);
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 trying to do something like -
List<int> accountList = new List<int>();
accountList .Add(1);
accountList .Add(27);
var rec = _db.Accounts.Where(a=> accountList.Contains(a.accountId)).Take(10);
My code is a little more complicated than this - there are several other conditions in the where clause, but this is the bit that is causing problems - nothing gets returned even when there are matching values.
Basically I want it to retrieve all the records where accountId matches a value in my list.
Any pointers?
The sample above is giving me a cant convert lambda error.
You are missing a bit on your contains version
var hold2 = _db.Accounts.Where(a => find.Contains(a.accountDd)).Take(10).ToList();
Have you tried using any
var hold2 = _db.Accounts.Where(a => accountList.Any(m => m == a.accountId)).Take(10).ToList();
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 6 years ago.
Improve this question
Building a list from model in C# using Entity Framework. I am using LINQ to try to match up the first 6 or 7 characters in a field. In a language other than C# I would use a regex expression.. or in SQL a "Like" with wildcard symbols. Below is the code I'm using, and I am getting an Argument exception.
List<InvoiceHeader> tempData = db.InvoiceHeader
.Where(f => f.ivh_invoicenumber.Any(t => f.ivh_invoicenumber.StartsWith(temp))).ToList();
If ivh_invoicenumber and temp are of type string
var tempData = db.InvoiceHeader.Where(f => f.ivh_invoicenumber.StartsWith(temp)).ToList();
If temp is array of strings:
var tempData = db.InvoiceHeader
.Where(f => temp.Any(t => f.ivh_invoicenumber.StartsWith(t))).ToList();