Convert String to linq condition Entity Framework core - linq

I have a Combobox with different conditions like "=", ">=", "<=" ... and dates.
I would like to use the conditions in strings to compare dates. It's possible to convert the operators in string format to logical operators to compare dates on linq query to Oracle Database?
I need dynamic conditions based on strings like "=", ">=" ...

You cannot compare a DateTime variable with a String variable.
So you need to create a DateTime. May design pattern can help a bit for your special case like Factory Pattern ;)
Look at
DbTransectionFunctions
Here is a sample codes
var entity = dbContext.MyTable
.Where(w => DbFunctions.TruncateTime(w.SavedDateOnDb) == model.SelectedDate)
.First();
Another sample like this after ef6 :)
var list = db.MyClass.Where(c=> DbFunctions.TruncateTime(c.DbrecordDate)
> DbFunctions.TruncateTime(DateTime.UtcNow));

Related

LINQ query to find items where multiple substrings are searched on one column

Select * from web_data where Title like "%Lawn%" || Title like "%silk%"......and so on.
Lawn, Silk etc are in List<web_data>. So i'm looking for a way to search those substrings in the Title column(discription property). One of them must be contained, then the row should be returned.
I've tried this
query = query
.Where(x => filter.FabricType.Any(f => x.discription.Contains(x.discription)))
.AsQueryable();
It's not working. That linq to sql code returns an error:
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
any alternatives?
You can use Contains() or You can also use .StartsWith() or .EndsWith().
collListItem.Where(x => x.discription.Contains("Lawn") || x.discription.Contains("silk")).ToList();

How do I filter in linq query when field needs parsing first?

I have a data table containing multiple columns and one column that stores somewhat complex text patterns - I need to parse the field to determine if a particular sub strings exist in specific positions within the larger string pattern and then if the record should be filtered out as a result.
I can't see a way to perform the parse other than by writing a C# parsing function with String.Split method calls, foreach, etc. But if I try to parse like this:
var myFilteredTable = _db.MyTable.Where(t => t.Column1 == 'Filter1'
&& ParseIsMyItemInColumn2(t) );
I get "has no supported translation to SQL" errors.
The other option I thought of was to build the initial result without the Parse:
var myFilteredTable = _db.MyTable.Where(t => t.Column1 == 'Filter1' );
and iterate through the IQueryable resultset, testing each row with the parse function, to filter out the unwanted rows, but IQueryable does not have Remove function to strip out unwanted rows nor Add function to allow me to build up a new resultset.
So how can I filter in linq when I also need to write a Parse function?
Well the "initial filter in the database then do the rest locally" is easy:
var filtered = _db.MyTable.Where(t => t.Column1 == "Filter1")
.AsEnumerable() // Do the rest locally
.Where(t => ParseIsMyItemInColumn2(t));
AsEnumerable is a simple pass through method, but because the result is typed as IEnumerable<T> rather than IQueryable<T>, the subsequent LINQ operations use the LINQ to Objects methods in Enumerable rather than the ones in Queryable.
Obviously if a lot of items match the first filter but fail the second, that won't be terribly efficient...
Unfortunately, if the "parse function" is not something that can be translated to SQL, you will need to pull the results and use LINQ to Objects:
var myFilteredTable = _db.MyTable.Where(t => t.Column1 == 'Filter1')
.AsEnumerable().Where(ParseIsMyItemInColumn2);
Note that this will stream all of the results into memory, and then perform your parse.

How do I query an integer column for "starts with" in Entity Framework?

I have a column that's defined as an integer in EF (Code First). I want to search it using "starts with." Now, I can do this:
Where(x => SqlFunctions.StringConvert((double)x.AccountNumber).StartsWith(searchTerm))
However, SqlFunctions.StringConvert() gets translated to the T-SQL function STR(), which left-pads the result for reasons which are beyond my comprehension.
Also, I can't use string.TrimStart() because it's not supported by the Entity Framework.
Can anyone lend any help?
Trim() and TrimStart() work in LINQ to Entities, so you can use:
Where(x => SqlFunctions.StringConvert((double)x.AccountNumber)
.TrimStart().StartsWith(searchTerm))
TrimStart translates into LTRIM in SQL. With searchTerm = 123 for example you get something like:
WHERE LTRIM(STR( CAST( [Extent1].[AccountNumber] AS float))) LIKE N'123%'

LINQ Convert string to datetime

In SQL I do it like this:
DateMonth=convert(datetime, convert(char(6), AASI.Inv_Acctcur) + '01')
How to do it in LINQ? String is in following format:
"yyyyMMdd"
Thanks,
Ile
EDIT:
Example of SQL usage:
SELECT convert(datetime, '20161023', 112) -- yyyymmdd
Found here: http://www.sqlusa.com/bestpractices/datetimeconversion/
This page on MSDN documentation lists all convert methods that LINQ to SQL does not support.
Convert.ToDateTime is not listed in there. So, I guess you could just use Convert.ToDateTime to do what you want.
This is not something typically for LINQ or any LINQ over Expression trees enabled provider (such as LINQ to SQL, LINQ to Entities, LINQ to NHibernate, LLBLGen Pro, etc, etc). This is simply a language question. You want to know how to convert the format yyyyMMdd to a DateTime.
The trick is to leave the conversion OUT of your LINQ (over Expression trees) query, because a LINQ provider will not be able to convert it, or if it can, you will get very provider specific implementation.
Therefore, the trick is to get it out of the database as a string (or of course even better: change your database model) and convert it to a DateTime in .NET. For instance:
// Doing .ToArray is essential, to ensure that the
// query is executed right away.
string[] inMemoryCollectionStrings = (
from item in db.Items
where some_condition
select item.Inv_Acctcur).ToArray();
// This next query does the translation from string to DateTime.
IEnumerable<DateTime> dates =
from value in inMemoryCollectionStrings
select DateTime.ParseExact(value, "yyyyMMdd",
CultureInfo.InvariantCulture);
In other words, you can use the following line to make the conversion:
DateTime.ParseExact(value, "yyyyMMdd", CultureInfo.InvariantCulture);

LINQ syntax where string value is not null or empty

I'm trying to do a query like so...
query.Where(x => !string.IsNullOrEmpty(x.PropertyName));
but it fails...
so for now I have implemented the following, which works...
query.Where(x => (x.PropertyName ?? string.Empty) != string.Empty);
is there a better (more native?) way that LINQ handles this?
EDIT
apologize! didn't include the provider... This is using LINQ to SQL
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=367077
Problem Statement
It's possible to write LINQ to SQL that gets all rows that have either null or an empty string in a given field, but it's not possible to use string.IsNullOrEmpty to do it, even though many other string methods map to LINQ to SQL.
Proposed Solution
Allow string.IsNullOrEmpty in a LINQ to SQL where clause so that these two queries have the same result:
var fieldNullOrEmpty =
from item in db.SomeTable
where item.SomeField == null || item.SomeField.Equals(string.Empty)
select item;
var fieldNullOrEmpty2 =
from item in db.SomeTable
where string.IsNullOrEmpty(item.SomeField)
select item;
Other Reading:
1. DevArt
2. Dervalp.com
3. StackOverflow Post
This won't fail on Linq2Objects, but it will fail for Linq2SQL, so I am assuming that you are talking about the SQL provider or something similar.
The reason has to do with the way that the SQL provider handles your lambda expression. It doesn't take it as a function Func<P,T>, but an expression Expression<Func<P,T>>. It takes that expression tree and translates it so an actual SQL statement, which it sends off to the server.
The translator knows how to handle basic operators, but it doesn't know how to handle methods on objects. It doesn't know that IsNullOrEmpty(x) translates to return x == null || x == string.empty. That has to be done explicitly for the translation to SQL to take place.
This will work fine with Linq to Objects. However, some LINQ providers have difficulty running CLR methods as part of the query. This is expecially true of some database providers.
The problem is that the DB providers try to move and compile the LINQ query as a database query, to prevent pulling all of the objects across the wire. This is a good thing, but does occasionally restrict the flexibility in your predicates.
Unfortunately, without checking the provider documentation, it's difficult to always know exactly what will or will not be supported directly in the provider. It looks like your provider allows comparisons, but not the string check. I'd guess that, in your case, this is probably about as good of an approach as you can get. (It's really not that different from the IsNullOrEmpty check, other than creating the "string.Empty" instance for comparison, but that's minor.)
... 12 years ago :) But still, some one may found it helpful:
Often it is good to check white spaces too
query.Where(x => !string.IsNullOrWhiteSpace(x.PropertyName));
it will converted to sql as:
WHERE [x].[PropertyName] IS NOT NULL AND ((LTRIM(RTRIM([x].[PropertyName])) <> N'') OR [x].[PropertyName] IS NULL)
or other way:
query.Where(x => string.Compare(x.PropertyName," ") > 0);
will be converted to sql as:
WHERE [x].[PropertyName] > N' '
If you want to go change the type of the collection from nullable type IEnumerable<T?> to non-null type IEnumerable<T> you can use .OfType<T>().
.OfType<T>() will remove null values and return a list of the type T.
Example: If you have a list of nullable strings: List<string?> you can change the type of the list to string by using OfType<string() as in the below example:
List<string?> nullableStrings = new List<string?> { "test1", null, "test2" };
List<string> strings = nullableStrings.OfType<string>().ToList();
// strings now only contains { "test1", "test2" }
This will result in a list of strings only containing test1 and test2.

Resources