Dynamic Linq and Where Clause & NOT Contains - linq

.Where("MyColumn.Contains(#0)", "column_value")
I would like to know the syntax or the correct way to have NOT contains in dynamic linq.

All of these worked for me:
collection.Where("!MyColumn.Contains(#0)", "value");
collection.Where("MyColumn.Contains(#0) = false", "value");
collection.Where("MyColumn.Contains(#0) == false", "value");
I used System.Linq.Dynamic (version 1.0.2) from NuGet.

Related

Sitecore data API, LINQ and language versions

We have a multi-language Sitecore installation and have discovered a small issue when using LINQ queries off of a Sitecore item. The issue is that it seems to ignore the context language / item language versions in the LINQ query (ie. it returns items that don't have a language version matching the current context language).
Here are two LINQ queries; neither of which work as expected:
var items = item.Children.Where(i => i.TemplateName == "Brochure")
.ToList();
var items = item.Children.Where(i => i.TemplateName == "Brochure" && i.Language == Sitecore.Context.Language)
.ToList();
If I change the statement to use Axes and a Sitecore query, it works as expected and items do not return if they do not have the appropriate language version:
var items = (item.Axes.SelectItems("./*[##templatekey='Brochure']") ?? Enumerable.Empty<Item>())
.ToList();
Has anyone got around this using LINQ, or would it be best to convert everything to Sitecore queries for statements like these?
Items have versions and versions exist for a specific language.
The item itself exists without a language.
If you only want the items that have a version in the context language, you need to check if it has versions:
var items = item.Children
.Where(i => i.TemplateName == "Brochure")
.Where(i => i.Versions.Count > 0)
.ToList();

Why does EntityFramework NOT use my predicate?

I have a PredicateBuilder which I got it from
http://www.albahari.com/nutshell/predicatebuilder.aspx
And here is how I use it:
var Expression = PredicateBuilder.True<UCM_UserAgent>();
Expression = Expression.And(item => item.AgentText == "TestUserAgent Value");
Func<UCM_UserAgent, bool> SearchCriteria = Expression.Compile();
var Data = Context.UCM_UserAgent
.Where(SearchCriteria)
.ToList();
And when I checked SQL Profiler on my database:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[AgentText] AS [AgentText],
FROM [dbo].[UCM_UserAgent] AS [Extent1]
Now, where is the condition I added? I could not solve this. Unfortunately, this Entity Framework and Predicate Builder - Predicates being Ignored in SQL Query did not help either.
Edit:
To eliminate any misunderstandings; I do not want to use as x => x.AgentText = "SomeText". Because I want to build a dynamic LINQ query, thus I use PredicateBuilder.
If you want the predicate to be hadled by LINQ to Entities you need to pass the Expression<Func<T,bool>> version to the Queryable.Where() method instead of passing the Func<T,bool> version to the Enumerable.Where() method as you are currently doing, i.e. do not compile the expression into IL before calling the Where method or you will necessarily get the LINQ to Objects version.

how to implement and, or clauses in sql queries? ruby on rails

I tried looking around but could not find an answer on how to implement the combination of AND, |(or). I am getting an error. The code is listed below:
allgames = GameInfo.Get.where(["list = ? AND (lang = ? OR lang = ?)", "yes", "en", #lang]).order(:order)
The error I am getting is below:
The specified query expression syntax is not valid.
please shed some light,
Regards,
Pipe is not used in sql:
where("list = ? AND (lang = ? OR lang = ?)", "yes", 'en',#lang)

Nhibernate linq. The where extension method does not add the where clause to the SQL command, why?

I want to add the where clause to a linq statement, but it doesn't behave as i would expected it to.
When i use this code:
IQueryable<Employee> EmpQuery = from e in Session.Query<Employee>() where e.Surname == "Test" select e;
EmpQuery.ToList();
or i use this code:
IQueryable<Employee> EmpQuery = (from e in Session.Query<Employee>() select e).Where(e => e.Surname == "Test");
EmpQuery.ToList();
The where clause is included in the SQL command, but when i try it this way:
IQueryable<Employee> EmpQuery = from e in Session.Query<Employee>() select e;
EmpQuery.Where(e => e.Surname == "Test");
The where clause is not included in the SQL command. Why is this? Is there another way to dynamically add criteria to a Nhibernate Linq query?
You're not using the return value of Where. LINQ is designed around functional concepts - calling Where doesn't modify the existing query, it returns a new query which applies the filter. The existing query remains as it was - which means you can reuse it for (say) a different filter.
Note that your current query expression (from x in y select x, effectively) is pretty pointless. I would suggest simply writing:
var query = Session.Query<Employee>().Where(e => e.Surname == "Test");
Just to clarify on Jon's remark, your implementation would be fine with the following tweak:
IQueryable<Employee> modifiedQuery = EmpQuery.Where(e => e.Surname == "Test");
Then just invoke the appropriate enumerator (ToList, ToArray, foreach) on modifiedQuery. And I wouldn't say that it create a complete new query, but instead creates a query which wraps around the original (kind of along the lines of the adapter pattern). Granted, your example doesn't need the additions, but this is how you would add additional criteria onto an existing LINQ expression, and that is what your question actually asked.

Dynamic LINQ query using an Attribute

I have had some success getting the MSFT Dynamic Linq stuff to work, but now I need to create a "Where" clause that includes an Attribute.
The error I get is "No applicable aggregate method 'First' exists"
Here is my code:
where = "Element(XName.Get(\"procedure\")).Attributes(XName.Get(\"code\")).First() = \"28002\"";
var q2 = doc.Elements().Descendants("vocabularybody").AsQueryable().Where(where);
if (q2 != null && q2.Count() > 0)
foundItems.Add(item);
here is my XML
<vocabulary>
<vocabularyheader>
<vocabularyid>5</vocabularyid>
<vocabularyname>Scheduled Procedure</vocabularyname>
</vocabularyheader>
<vocabularybody>
<procedure code="28002" type="Surgery"/>
</vocabularybody>
</gazoontvocabulary>
I'm not familiar with the Dynamic LINQ library yet but shouldn't you need the equality operator (==) and not the assignment operator (=) for the where clause?

Resources