I'm trying to simplify and optimize the following lambda expression. My requirement is to get the first lead whose mobile phone or telephone1 matches intakePhoneNum. I want to match first 10 digits only.
Entity matchingLead =
allLeads.Where(l =>
(l.Attributes.Contains("mobilephone") &&
(Regex.Replace(l.Attributes["mobilephone"].ToString(), "[^0-9]", "").Length >=10
? Regex.Replace(l.Attributes["mobilephone"].ToString(), "[^0-9]", "").Substring(0,9)
: Regex.Replace(l.Attributes["mobilephone"].ToString(), "[^0-9]", "").Substring(0)).Equals(intakePhoneNum))||
(l.Attributes.Contains("address1_telephone1") &&
(Regex.Replace(l.Attributes["address1_telephone1"].ToString(), "[^0-9]", "").Length >= 10
? Regex.Replace(l.Attributes["address1_telephone1"].ToString(), "[^0-9]", "").Substring(0, 9)
: Regex.Replace(l.Attributes["address1_telephone1"].ToString(), "[^0-9]", "").Substring(0)).Equals(intakePhoneNum))).FirstOrDefault();
First, I would suggest to introduce variables for the attributes.
Then, instead of the differentiation between Length >= 10 and Length < 10, simple use StartsWith.
And last, instead of Where(...).FirstOrDefault, simply use FirstOrDefault(...)
Entity matchingLead =
allLeads.FirstOrDefault(l =>
{
if (l.Attributes.Contains("mobilephone"))
{
var phone = Regex.Replace(l.Attributes["mobilephone"].ToString(), "[^0-9]", "");
if (phone.StartsWith(intakePhoneNum))
return true;
}
if (l.Attributes.Contains("address1_telephone1"))
{
var phone = Regex.Replace(l.Attributes["address1_telephone1"].ToString(), "[^0-9]", "");
if (phone.StartsWith(intakePhoneNum))
return true;
}
return false;
});
Related
I would like to find string numbers between "0000" and "9999" inside larger strings
from "qwt0000abc" to "qwt9999abc".
MSSQL where id between "qwt0000abc" and "qwt9999abc"
How I would write the same LINQ query?
var selectedOrders = orders.Where(f => f.Id between "qwt0000abc" and "qwt9999abc");
Assuming you only want rows with that same prefix and suffix, then just replace between like this:
var selectedOrders = orders.Where(f => f.Id.CompareTo("qwt0000abc") >= 0 && f.Id.CompareTo("qwt9999abc") < 0).ToList() ;
And if this is EF you can perhaps use Like:
var selectedOrders = orders.Where(f => EF.Functions.Like(f.Id, "qwt[0-9][0-9][0-9]abc")).ToList();
Try this:
var selectedOrders = arr.Where(x => Regex.IsMatch(x, ".*[0-9]{4}.*"));
or this, if you need exactly qwt prefix and abc suffix.
var selectedOrders = arr.Where(x => Regex.IsMatch(x, "qwt[0-9]{4}abc"));
.* here is any literal any number of times(include zero), [0-9]{4} is digit between 0 and 9, exactly 4 digits.
I'm checking a string value using Linq, if it has all digits using the following query:
bool isAllDigits = !query.Any(ch => ch < '0' || ch > '9');
But if user enters a space, along with digits (eg: "123 456", "123456 ") this check fails. Is there a way that I can check for white spaces as well?
I don't want to trim out the white spaces, because I use this text box to search with text, which contains spaces in between.
Try this:
bool isAllDigits = query.All(c => char.IsWhiteSpace(c) || char.IsDigit(c));
These methods are built into the framework and instead of rolling your own checks, I would suggest using these.
I made a fiddle here to demonstrate.
EDIT
As pointed out in the comments, char.IsDigit() will return true for other "digit" characters as well (i.e not just '0'-'9', but also other language/culture number representations as well). The full list (I believe) can be found here.
In addition, char.IsWhiteSpace() will also return true for various types of whitespace characters (see the docs for more details).
If you want to only allow 0-9 and a regular 'ol space character, you can do this:
bool isAllDigits = s.All(c => (c < 57 && c > 48) || c == 32);
I am using the decimal values of the ASCII characters (for reference), but you could also continue doing it the way you are currently:
bool isAllDigits = s.All(c => (c < '9' && c > '0') || c == ' ');
Either way is fine, the important thing to note is the parentheses. You want any character that is greater than 0, but less than 9 OR just a single space.
bool isAllDigits = !query.Any(ch => (ch < '0' || ch > '9') && ch != ' ');
Im trying to detect if my string has a lenght > 4 convert it to my local currency otherwise make it display no decimal's something like this:
This is my code:
var listDay = data.Where(c => c.Fecha >= actualDate && c.Fecha <= actualDateMax).ToList();
var haveDataDay = listDay.Count() > 0;
<h4 class="semi-bold">
#if (haveDataDay)
{ #valueDateDay.ToString("F2") }
else
{ #this.FP("lbl.loader.nodata") }
</h4>
But i dont know how to check this
Someone has any idea?
There's probably a more elegant way to do this but this works just fine:
var valueDateDay = 1000;
Console.WriteLine(valueDateDay.ToString(valueDateDay.ToString().Length > 4 ? "N0" : "C2"));
valueDateDay = 100000;
Console.WriteLine(valueDateDay.ToString(valueDateDay.ToString().Length > 4 ? "N0" : "C2"));
See it in action
Not sure if I understand what your variables mean but try:
string currency;
currency = valueDateDay.ToString().Lenght > 4 ? "NO" : "C2";
How to format a number such that first 6 digits and last 4 digits are not hidden
such that 111111111111111 looks like 111111****1111
You can also use LINQ, substituting chars with indexes more than 5 and less than number.Length - 4 with *:
string number = "111111111111111";
string res = new string(number.Select((c, index) => { return (index <= 5 || index >= number.Length - 4) ? c : '*'; }).ToArray());
Console.WriteLine(res); // 111111*****1111
One simple way to do this is to slit the input..
int number = 111111111111111;
string firstsix = number.ToString().Substring(0,6) //gets the first 6 digits
string middlefour = number.ToString().Substring(6,4) //gets the next 4
string rest = number.ToString().Substring(10, number.ToString().Lenght) //gets the rest
string combined = firstsix + "****" +rest;
You need to use \G anchor in-order to do a continuous string match.
string result = Regex.Replace(str, #"(?:^(.{6})|\G).(?=.{4})", "$1*");
DEMO
IDEONE
I want to add one space after every two characters, and add a character in front of every single character.
This is my code:
string str2;
str2 = str1.ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString());
I have no problems separating every two characters with one space, but how do I know if the separated string has an individual character, and add a character infront of that character?
I understand that my question is confusing as I'm not sure how to put what I want in words..
So I'll just give an example:
I have this string:
0123457
After separating every two characters with a space, I'll get:
01 23 45 7
I want to add a 6 infront of the 7.
Note: Numbers are dependent on user's input, so it's not always the same.
Thanks.
[TestMethod]
public void StackOverflowQuestion()
{
var input = "0123457";
var temp = Regex.Replace(input, #"(.{2})", "$1 ");
Assert.AreEqual("01 23 45 7", temp);
}
Try something like this:
static string ProcessString(string input)
{
StringBuilder buffer = new StringBuilder(input.Length*3/2);
for (int i=0; i<input.Length; i++)
{
if ((i>0) & (i%2==0))
buffer.Append(" ");
buffer.Append(input[i]);
}
return buffer.ToString();
}
Naturally you'd need to add in some logic about the extra numbers, but the basic idea should be clear from the above.
May be you can try, if i right understand your request,
String.Length % 2
if result is 0, you done with first iteration, if not, just add a character infront of last one.
I think this is what you asked for
string str1 = "3322356";
string str2;
str2 = String.Join(" ",
str1.ToCharArray().Aggregate("",
(result, c) => result += ((!string.IsNullOrEmpty(result) &&
(result.Length + 1) % 3 == 0) ? " " : "") + c.ToString())
.Split(' ').ToList().Select(
x => x.Length == 1
? String.Format("{0}{1}", Int32.Parse(x) - 1, x)
: x).ToArray());
result is "33 22 35 56"