Query whether string field contains digit in Entity Framework - entity-framework

How do I query for all entities where a string column contains a digit in Entity Framework?
e.g.
"100 Main Street" // match
"Will advise" // no match
I really don't want to do this:
myclass.Property.Contains("0")
|| myclass.Property.Contains("1")
|| myclass.Property.Contains("2")
|| myclass.Property.Contains("3")
|| myclass.Property.Contains("4")
|| myclass.Property.Contains("5")
|| myclass.Property.Contains("6")
|| myclass.Property.Contains("7")
|| myclass.Property.Contains("8")
|| myclass.Property.Contains("9")

You could try using Contains method on array of digits:
var digits = Enumerable.Range(0, 10).Select(s=>s.ToString()).ToArray();
var result = YOURCONTEXT.YOUR_TABLE
.Where(n => digits.Any(d =>
n.YOUR_FIELD.Contains(d)))
.ToList();

You can use SqlFunctions.PatIndex to check if the column contains a digit:
where SqlFunctions.PatIndex("%[0-9]%", myClass.Property) != 0

Related

EF Order query by matched filter

I want to find the way to achieve order prioritization by matched field.
For example:
var query = ctx.Set<Article>()
.Where(e => e.Title == "search query" || e.Content == "search query")
.OrderBy(???)
.Take(4);
Is it possible to order the result in such a way that records matched by Title
was on top, and entries matched by Content was on bottom. In single query which requests only 4 records from database?
like this
Title Content
------------------------------
search query other text
search query other text
other text search query
other text search query
Sure it is possible. You just order by conditional expression yielding "priority" based on your match conditions. You need to replicate all except the last condition there though. Something like (pseudo code):
.Where(e => cond1(e) || cond2(e) || ... cond{N-1}(e) || condN(e))
.OrderBy(e => cond1(e) ? 0 : cond2(e) ? 1 : ... cond{N-1}(e) ? N - 1 : N)
With your sample:
string match = "search query";
var query = ctx.Set<Article>()
.Where(e => e.Title == match || e.Content == match)
.OrderBy(e => e.Title == match ? 0 : 1) // first by priority
.ThenBy(e => ...) // then by others if needed
.Take(4);
You can also combine multiple OrderBy at once like this:
string searchQuery = "search query";
int wantedResults = 4:
var query = ctx.Set<Article>()
.Where(e => e.Title.Equals(searchQuery) || e.Content.Equals(searchQuery))
.OrderBy(e => new { e.Title, e.Content })
.Take(wantedResults);

Optional compare in query

Hi i have a problem with my query because i want select that items which brands are called in string subcategory. But if that subcategory is equal "none" i would to select them all i want to do it in query not in linq. Here's my function
string subcategory = HttpContext.Current.Session["subcategory"].ToString() == "none" ? "" : HttpContext.Current.Session["subcategory"].ToString();
List<int> processors = (from x in context.Processors
where (x.Price >= filters.PriceMin && x.Price <= filters.PriceMax)
where x.Brand == subcategory
select x.Product_ID).ToList();
The pattern for this in LINQ and EF is to build up the query differently for the two cases before the query is executed by calling IQueryable.ToList(); eg:
string subcategory = ...;
var q = from x in context.Processors
where (x.Price >= filters.PriceMin && x.Price <= filters.PriceMax)
select x;
if (subcategory != "none")
{
q=q.Where(x => x.Brand == subcategory);
}
var processors = q.Select(x => x.Product_ID).ToList();

EF Conditional Query to match with whatever information available doesn't work

I am trying to write a query to search employees by whatever of the 3 attributes are available in my object using Linq to Entity.
My object has First name, Last name, and Date of Birth.
My query looks like this (search is the object containing partial or full info)
var results = ctx
.emp
.Where(p =>
p.Fname == (search.Firstname ?? p.Fname)
&& p.Lname == (search.Lastname ?? p.Lname)
&& p.Dob == (search.Dob ?? p.Dob ))
The issue is that above query is matching employees with null values in each of those 3 columns along with the matching value. For instance if I provide search.Firstname to be John, it matches everyone with first name John AND everyone with null in first name.
How do I not match nulls in fields where a value is provided?
Haven't tested, but won't something like this work.
var results = ctx
.emp
.Where(p => (search.Firstname == null ? p.Fname == p.Fname : p.Fname == search.Firstname) ||
(search.Lastname == null ? p.Lname == p.Lname : p.Lname == search.Lastname) ||
(search.Dob == null ? p.Dob == p.Dob : p.Dob == search.Dob))
Please excuse the formatting. Using my mobile :)

Linq query on a Date giving an error "Nullable object must have a value."

I have a list of objects that I'm passing into a function where I want to filter out records for a specific company and if a DateTime field is today.
I'm getting an error "Nullable object must have a value." when I do and I believe it is due to the fact that some of the "Verified_Date" fields are null, but not all of them.
This is the LINQ I came up with. Is there a way that I can filter out on a specific company and if there Verified_Date = today even if some of the records have a Verified_Date = NULL?
var today = DateTime.Today;
var listFiltered = data.Where(d => d.CompanyCode == companyCode && d.VerifiedDate.Value.Date == today).ToList();
You can try this:
var today = DateTime.Today;
var listFiltered = data.Where(d => d.CompanyCode == companyCode
&& d.VerifiedDate!=null
&& d.VerifiedDate.Value.Date == today).ToList();
Check that VerifiedDate isn't null in your Where condition.
var listFiltered = data.Where(d => d.CompanyCode == companyCode
&& d.VerifiedDate != null
&& d.VerifiedDate.Value.Date == DateTime.Today)
.ToList();

Filter condition not working properly on list (C#3.0)

I have a datatable that has many NULL or " " strings. Next I am type casting the DataTable to a list .
Now if I want to filter those conditions on this list and get the resultant value(without NULL or String.Empty or " " records) what should I do?
My code
DataTableExtensions.AsEnumerable(dt).ToList().ForEach(i =>
{
if (i[0] != null)
{
if ((i[0].ToString() != string.Empty)|| (i[0].ToString() != " "))
{
list = dt.AsEnumerable().ToList();
}
}
});
But I am getting all the records. It is not getting filtered.
Using C#3.0
Please help
Thanks
You're looking at i[0] so I'm going to assume that you're only interested in the first column of your table:
var rows = dt.AsEnumerable()
.Where(r => !r.IsNull(0) // check for DBNull
&& r[0] != null
&& r[0].ToString().Trim().Length > 0)
.ToList();
So that looks at every row but only returns the rows where the first column has a value and the string value is not empty or whitespace.