How to get something like sql '%test%words%' in linq - linq

I know how to use .Contains for looking up '%testwords%', my question is about how to use linq to get '%test%words%'. Thanks

maybe something like this if you're not using Linq to SQL:
var query = from o in yourObject
where o.field.Contains("test")
where o.field.Contains("words")
where o.field.indexOf("test") < o.field.indexOf("words")
select o;
if you are using Linq to SQL, use the SqlMethods that Stefanvds showed.

.Where(q => SqlMethods.Like(q,"%test%words%"))
use SqlMethods

well, i think the answer is NO :(
Probably the closest answer is lst.Where(foo.Contains("test") && foo.Contains("word")), though that statement doesnt give the right sequence as '%test%word%'

Related

Foreach delete with linq

The following statement should delete someCars from allCars, and it works, however I was wondering if there is a better solution which specifically uses only linq
foreach (var car in someCars)
{
db.allCars.DeleteObject(car);
}
Note that db is the instance of the db entities.
Any help would be much appreciated.
You can use EntityFramework.Extended library to delete entities based on some condition (library available from Nuget):
db.allCars.Delete(car => /*condition*/);
using Linq as ForEach:
db.allCars.Where(/* condition */).ToList().ForEach(car => db.allCars.Remove(car));
in the same line of this answer :) , How do I delete multiple rows in Entity Framework (without foreach)
You can use:
db.allCars.RemoveAll(db.allCars.Where(/* condition */));

Doctrine query not executing

trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....

EntityFramework Linq query

I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much
You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.
Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.

How to apply LINQ to ObjectResult<int?> when using EF 4.0?

Suppose I have a function call from EF like:
var result = context.myFunction();
the result type is ObjectResult<int?>. Then I want to use linq to get the single value.
How to write the linq?
Maybe something like this
result.SkipWhile(o => !o.HasValue).Take(1)

DBnull in Linq query causing problems

i am doing a query thus:
int numberInInterval = (from dsStatistics.J2RespondentRow item in jStats.J2Respondent
where item.EndTime > dtIntervalLower && item.EndTime <= dtIntervalUpper
select item).count();
there appear to be some dbnulls in the endtime column..
any way i can avoid these?
tried adding && where item.endtime != null.. and even != dbnull.value
do i have to do a second (first) query to grab all that arent null then run the above one?
im sure its super simple fix, but im still missing it.. as per
thanks
nat
I think you want to use item.EndTime.HasValue, and not item.EndTime == null.
The simplest way to do it is to use .GetValueOrDefault(...Some reasonable default...) on the value that can be null and it will avoid the error.
The way I typically do this in a T-SQL query is to use ISNULL on the date, and set the date to something like '12/31/2099' when it's null.
With Linq it could be something like this:
from t in MyTable
where
Convert.ToString((Convert.ToString(t.MyDateColumn) ?? "12/31/2099")) < MyTargetValue

Resources