We are trying to replace multiple character in property(string) that we query from database.
var list = _context.Users.Where(t => t.Enable).AsQueryable();
list = list.Where(t => t.Name.ToLower().Contains(searchValue));
Property Name should be without characters (.,-').
We have tried:
list = list.Where(t => t.Name.ToLower().Replace(".","").Replace(",","").Replace("-","").Contains(searchValue));
and it works like this, but we don't want to use replace multiple times.
Is there any other ways that works with IQueryable? Thanks.
We have decided to do it in database,SQL, creating View like this:
CREATE OR ALTER VIEW Users_View
AS
SELECT Id,CreationDate, UserName = REPLACE(TRANSLATE(Users.UserName, '_'',.-', '#####'), '#', '')
FROM Users;
and than we just do query on view, like this UserName is already without special characters.
Well, the first thing you want to do is add a column to your table called "CanonicalName"
Then your query becomes just:
var list = (from t in _context.Users
where t.Enable && t.CanonicalName.Contains(searchValue)
select t).ToList();
Now, you need to populate CanonicalName. Since you only have to do this once ever for each record, it doesn't have to be that efficient, but here goes:
public string Canonicalize(string str)
{
var sb = new StringBuilder(str.Length);
foreach(var c in str)
{
if (c == '.' ||
c == ',' ||
c == '-') // you may wish to add others
continue;
c = Char.ToLower(c);
sb.Append(c);
}
return sb.ToString();
}
UPDATE: Since people need everything spelled out...
foreach(var u in _context.Users)
u.CanonicalName = Canonicalize(u.Name);
_context.SaveChanges();
Related
I'm curious as to why my linq group by query returns 417 results whereas my SQL interpretation returns 419? I'm looking for duplicated emails from my list. I've checked out the result set and the two email addresses that are missing from the linq set both have accents. Does linq not recognize accents? Is there a workaround? The email field type is a nvarchar(100).
Let me know if you have any questions,
Thanks in advance!
var listOfContacts = (from contacts in something
where contacts.Team.Id.Equals(TeamGuid) && !contacts.Email.Equals(null)
select new {contacts.Id, EmailAddress = contacts.Email.ToLower()}).ToList();
//Full Contact List; exact amount matches
var dupeEmailsList = listOfContacts
.GroupBy(x => x.EmailAddress)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
//Returns 417
SELECT Email, COUNT(*)
FROM something
WHERE Team = 'Actual Team Guid Inserted Here'
GROUP BY Email
HAVING (COUNT(LOWER(Email)) > 1 AND Email IS NOT NULL)
ORDER BY Email
//Returns 419
This is a known issue and the workaround has already been answered -> here and here
You have to explicitly tell it to ignore them.
This is from the links provided by #Bactos.
You just need to strip out what's called Diacritics, using built in C# normalization and CharUnicodeInfo.
You'll just have to make the call for each email address like so:
var listOfContacts = (from contacts in something
where contacts.Team.Id.Equals(TeamGuid) && !contacts.Email.Equals(null)
select new { contacts.Id, EmailAddress = CleanUpText(contacts.Email) }).ToList();
and the method you would need would be as follows:
private static string CleanUpText(string text)
{
var formD = text.Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (var ch in formD)
{
var uc = CharUnicodeInfo.GetUnicodeCategory(ch);
if (uc != UnicodeCategory.NonSpacingMark)
{
sb.Append(ch);
}
}
return sb.ToString().Normalize(NormalizationForm.FormC).ToLower();
}
Hope that helps!
Because of the .ToList() in your first LINQ expression, the GROUP BY is being performed within C# on the result of Email.ToLower()
This is not at all the same as the SQL query you give, where the GROUP BY is performed on the original EMAIL column, without the ToLower(). It is not surprising that the queries return different results.
I think you can try to ignore NULL values in the SELECT clause.In your LINQ query you are ingnoring NULLs.
I have the following code which extends my filter functionality to allow me to search for more than one Zip-code. Ex. (1254,125,145,1455)
Current Code:
if (Post_Code_PAR != null)
{
String[] PostCode_Array = Post_Code_PAR.Split(',');
query = from p in query where PostCode_Array.Contains(p.Post_Code) select p;
}
I want to extend this code in a way that if I give it something like (0*) it will find zip codes which starts with 0. If I give it 123* it will give me all zip codes which starts with 123. Thanks a lot.
use regular expression:
https://msdn.microsoft.com/en-us/library/ms228595.aspx
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
example:
IEnumerable<string> allInputs = new[] {"70000", "89000", "89001", "90001"};
string commaSeparatedPostCodePatterns = #"89000 , 90\d\d\d";
if (string.IsNullOrWhiteSpace(commaSeparatedPostCodePatterns))
return;
string[] postCodePatterns = commaSeparatedPostCodePatterns
.Split(',')
.Select(p => p.Trim()) // remove white spaces
.ToArray();
var allMatches =
allInputs.Where(input => postCodePatterns.Any(pattern => Regex.IsMatch(input, pattern)));
foreach (var match in allMatches)
Console.WriteLine(match);
With such a problem, the initial requirements are very simple but quickly become more and more complex (due to internationalization, exceptions to the rule, some smart tester testing an unexpected limit-case like 90ยง$% ...). So regular expressions provide the best trade-off simplicity vs. extendability
The regex equivalent to * is .*. But in your case, you would rather need more restrictive patterns and use the \d placeholder matching a single digit. Optional digit would be \d?. Zero or more digits would be \d*. One or more digits would be \d+. Please look at the documentation for more details.
You can replace the * with \d* which is the Regex way of saying "any amount of digits", basically. Then run through and filter with Regex.IsMatch().
if (Post_Code_PAR != null)
{
var PostCode_Array = from p in Post_Code_PAR.Split(',') select String.Format("^{0}$", p.Replace("*", #"\d*"));
query = (from p in query where PostCode_Array.Any(pattern => Regex.IsMatch(p.Post_Code, pattern)) select p).ToArray();
}
I tried to keep it as close to your code as possible but you could clean it up a bit and use lambda instead. But this should work just as well :)
string Post_Code_PAR = "";
String[] PostCode_Array = Post_Code_PAR.Split(',');
var zipCodeList = query.Where(x => CustomFilter(x.Post_Code, PostCode_Array)).ToList();
private static bool CustomFilter(string code, string[] foundZipCodes)
{
return foundZipCodes.Any(x =>
{
x = x.Trim();
var text = x.Replace("*", "");
if (x.EndsWith("*"))
return code.StartsWith(text);
if (x.StartsWith("*"))
return code.EndsWith(text);
if (x.Contains("*"))
return false;
return code.StartsWith(text);
});
}
now user can find: "0*,*52" etc.
var j = from c in User.USERs
where (c.USER_NAME.Equals(tempUserName))
select c;
this keeps on giving me an empty sequence
both are just strings im comparing user input with database
Do something like this:
var j = User.USERs.First(c => c.USER_NAME == tempUserName)
or
var j = User.USERs.Single(c => c.USER_NAME == tempUserName)
or just take j[0] from the result your own query gives you.
P.S. - both First or Single will throw an exception if no item matched the query, if you want to get null returned if nothing was found use FirstOrDefault respectively SingleOrDefault.
to broaden the spectrum try something like this:
string userToSearchFor = tempUserName.Trim().ToLower();
var j = User.USERs.FirstOrDefault(c => c.USER_NAME.ToLower() == userToSearchFor);
if (j != null)
{
//found something
}
If it is returning an empty sequence then your where clause is evaluating to false, check what SQL it is generating if you need to solve that problem first.
To answer your question, to get a single element you usually use
.Single()
.SingleOrDefault()
.First()
.FirstOrDefault()
I would do it like this:
var result = User.USERs.SingleOrDefault(x => x.USER_NAME.Equals(tempUserName));
if (result != null)
{
//do your thing
}
Can you help me with next: I'd like to add variable count of SqlMethods.Like() in one query?
For example: I've four words in list catCode, how can I create LINQ query with four SqlMethods.Like()?
using (var db = new MappingDataContext(_connection))
{
db.ObjectTrackingEnabled = false;
return (
from r in db.U_CDW_REPORTs
where (catCode.Length > 0 ?
SqlMethods.Like(r.CATEGORY, catCode) : r.CATEGORY != "*")
where r.SI_QTY > 0
orderby r.SI_QTY descending
select new Item
{
...
}).ToList();
}
What you need is dynamically OR-ing SqlMethod.Like operations. You need the PredicateBuilder.
Update: Here is an example of how to use the predicate builder.
string[] searchItems =
new string[] { "Pattern1", "Pattern2", "Pattern3" };
var likeExpression = PredicateBuilder.False<U_CDW_REPORT>();
foreach (string searchItem in searchItems)
{
var searchPattern = "%" + searchItem + "%";
likeExpression = likeExpression.Or(r =>
SqlMethods.Like(r.CATEGORY, searchPattern));
}
return (
from r in db.U_CDW_REPORTs.Where(likeExpression)
where r.SI_QTY > 0
orderby r.SI_QTY descending
select new Item { ... }).ToList();
You mean you have 4 disjoint LIKE clauses?
from entityRow in ctx.MyEntity
where (SqlMethods.Like(entityRow.Col, "%Pattern1%")
|| SqlMethods.Like(entityRow.Col, "%Patte%rn2%")
|| SqlMethods.Like(entityRow.Col, "Pattern3%")
|| SqlMethods.Like(entityRow.Col, "%Patte%rn4%"))
select entityRow;
UPDATE:
In that case, take a look at this: LINQ for LIKE queries of array elements
EDIT: made the code simpler. Also notice the updated explanation below.
You can build upon the query with multiple Where clauses in the following manner. Note that this approach ANDs the Where clauses, meaning all search terms will need to exist. In other words it's similar to ... where (condition1) && (condition2) && (conditionN).
string[] words = { "A", "B", "C" };
var query = dc.Products.AsQueryable(); // gives us an IQueryable<T> to build upon
foreach (var s in words)
{
query = query.Where(p => SqlMethods.Like(p.ProductName, "%" + s + "%"));
}
foreach (var item in query)
{
Console.WriteLine(item.ProductName);
}
The idea is to set the first part of the query up, then loop over the search terms and update the query. Of course you should update your pattern as needed; I used %word% for illustration purposes only.
I am trying to use Linq2Sql to return all rows that contain values from a list of strings. The linq2sql class object has a string property that contains words separated by spaces.
public class MyObject
{
public string MyProperty { get; set; }
}
Example MyProperty values are:
MyObject1.MyProperty = "text1 text2 text3 text4"
MyObject2.MyProperty = "text2"
For example, using a string collection, I pass the below list
var list = new List<>() { "text2", "text4" }
This would return both items in my example above as they both contain "text2" value.
I attempted the following using the below code however, because of my extension method the Linq2Sql cannot be evaluated.
public static IQueryable<MyObject> WithProperty(this IQueryable<MyProperty> qry,
IList<string> p)
{
return from t in qry
where t.MyProperty.Contains(p, ' ')
select t;
}
I also wrote an extension method
public static bool Contains(this string str, IList<string> list, char seperator)
{
if (str == null) return false;
if (list == null) return true;
var splitStr = str.Split(new char[] { seperator },
StringSplitOptions.RemoveEmptyEntries);
bool retval = false;
int matches = 0;
foreach (string s in splitStr)
{
foreach (string l in list)
{
if (String.Compare(s, l, true) == 0)
{
retval = true;
matches++;
}
}
}
return retval && (splitStr.Length > 0) && (list.Count == matches);
}
Any help or ideas on how I could achieve this?
Youre on the right track. The first parameter of your extension method WithProperty has to be of the type IQueryable<MyObject>, not IQueryable<MyProperty>.
Anyways you dont need an extension method for the IQueryable. Just use your Contains method in a lambda for filtering. This should work:
List<string> searchStrs = new List<string>() { "text2", "text4" }
IEnumerable<MyObject> myFilteredObjects = dataContext.MyObjects
.Where(myObj => myObj.MyProperty.Contains(searchStrs, ' '));
Update:
The above code snippet does not work. This is because the Contains method can not be converted into a SQL statement. I thought a while about the problem, and came to a solution by thinking about 'how would I do that in SQL?': You could do it by querying for each single keyword, and unioning all results together. Sadly the deferred execution of Linq-to-SQL prevents from doing that all in one query. So I came up with this compromise of a compromise. It queries for every single keyword. That can be one of the following:
equal to the string
in between two seperators
at the start of the string and followed by a seperator
or at the end of the string and headed by a seperator
This spans a valid expression tree and is translatable into SQL via Linq-to-SQL. After the query I dont defer the execution by immediatelly fetch the data and store it in a list. All lists are unioned afterwards.
public static IEnumerable<MyObject> ContainsOneOfTheseKeywords(
this IQueryable<MyObject> qry, List<string> keywords, char sep)
{
List<List<MyObject>> parts = new List<List<MyObject>>();
foreach (string keyw in keywords)
parts.Add((
from obj in qry
where obj.MyProperty == keyw ||
obj.MyProperty.IndexOf(sep + keyw + sep) != -1 ||
obj.MyProperty.IndexOf(keyw + sep) >= 0 ||
obj.MyProperty.IndexOf(sep + keyw) ==
obj.MyProperty.Length - keyw.Length - 1
select obj).ToList());
IEnumerable<MyObject> union = null;
bool first = true;
foreach (List<MyObject> part in parts)
{
if (first)
{
union = part;
first = false;
}
else
union = union.Union(part);
}
return union.ToList();
}
And use it:
List<string> searchStrs = new List<string>() { "text2", "text4" };
IEnumerable<MyObject> myFilteredObjects = dataContext.MyObjects
.ContainsOneOfTheseKeywords(searchStrs, ' ');
That solution is really everything else than elegant. For 10 keywords, I have to query the db 10 times and every time catch the data and store it in memory. This is wasting memory and has a bad performance. I just wanted to demonstrate that it is possible in Linq (maybe it can be optimized here or there, but I think it wont get perfect).
I would strongly recommend to swap the logic of that function into a stored procedure of your database server. One single query, optimized by the database server, and no waste of memory.
Another alternative would be to rethink your database design. If you want to query contents of one field (you are treating this field like an array of keywords, seperated by spaces), you may simply have chosen an inappropriate database design. You would rather want to create a new table with a foreign key to your table. The new table has then exactly one keyword. The queries would be much simpler, faster and more understandable.
I haven't tried, but if I remember correctly, this should work:
from t in ctx.Table
where list.Any(x => t.MyProperty.Contains(x))
select t
you can replace Any() with All() if you want all strings in list to match
EDIT:
To clarify what I was trying to do with this, here is a similar query written without linq, to explain the use of All and Any
where list.Any(x => t.MyProperty.Contains(x))
Translates to:
where t.MyProperty.Contains(list[0]) || t.MyProperty.Contains(list[1]) ||
t.MyProperty.Contains(list[n])
And
where list.Any(x => t.MyProperty.Contains(x))
Translates to:
where t.MyProperty.Contains(list[0]) && t.MyProperty.Contains(list[1]) &&
t.MyProperty.Contains(list[n])