Sitecore ContentSearch LINQ Unsupported expression node type: Parameter - linq

I get the following exception message when trying to run the ToList method for the Sitecore ContentSearch LINQ query in the following code block:
Unsupported expression node type: Parameter. This could be due to ordering of
expression statements. For example filtering after a select expression like this :
queryable.Select(i => new { i.Id, i.Title }).Where(i => d.Title > "A"
public virtual List<SearchResultItem> RunQuery(SearchParam param, bool showAllVersions, bool firstLoad)
{
Assert.ArgumentNotNull(Index, "Sitecore.SharedSource.Search");
var resultCollection = new List<SearchResultItem>();
try
{
using (var context = this.Index.CreateSearchContext())
{
var result = context.GetQueryable<SearchResultItem>()
.Where(x => HasFullText(x, param.FullTextQuery) &&
HasLanguage(x, param.Language) &&
HasRelation(x, param.RelatedIds) &&
HasTemplate(x, param.TemplateIds) &&
HasLocation(x, param.LocationIds)
);
resultCollection = result.ToList();
}
}
catch (Exception exception)
{
Log.Error(exception.StackTrace, this);
throw;
}
return resultCollection;
}
I can't figure out what causes this issue and I can't seem to reproduce the issue with standard .NET LINQ queries, in a standard Console Application (source code at the end).
Here is the source code for the HasLanguage, HasRelation, HasTemplate and HasLocation functions. I expect it has something to do with those because when I remove them and replace them with their implementation (where possible) I get no errors. However, when left inside the query they are not even accessed (tried debugging):
protected bool HasRefinements(SearchResultItem pseudoResult, SafeDictionary<string> refinements)
{
if (refinements.Count <= 0) return false;
foreach (var refinement in refinements)
{
var fieldName = refinement.Key.ToLowerInvariant();
var fieldValue = refinement.Value;
if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(fieldValue)))
{
return true;
}
}
return false;
}
protected bool HasLanguage(SearchResultItem pseudoResult, string language)
{
if (String.IsNullOrEmpty(language)) return false;
return pseudoResult.GetField(BuiltinFields.Language).Equals(language.ToLowerInvariant());
}
protected bool HasFullText(SearchResultItem pseudoResult, string searchText)
{
if (String.IsNullOrEmpty(searchText)) return false;
return pseudoResult.Content.Contains(searchText);
}
protected bool HasId(SearchResultItem pseudoResult, string fieldName, string filter)
{
if (String.IsNullOrEmpty(fieldName) || String.IsNullOrEmpty(filter)) return false;
var values = IdHelper.ParseId(filter);
foreach (var value in values.Where(ID.IsID))
{
if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(value)))
{
return true;
}
}
return false;
}
protected bool HasTemplate(SearchResultItem pseudoResult, string templateIds)
{
if (String.IsNullOrEmpty(templateIds)) return false;
templateIds = IdHelper.NormalizeGuid(templateIds);
return pseudoResult.TemplateId.ToString().Equals(templateIds);
}
protected bool HasLocation(SearchResultItem pseudoResult, string locationIds)
{
return HasId(pseudoResult, BuiltinFields.Path, locationIds);
}
protected bool HasRelation(SearchResultItem pseudoResult, string ids)
{
return HasId(pseudoResult, BuiltinFields.Links, ids);
}
And here is the source code for my test application using regular LINQ queries:
static void Main(string[] args)
{
Program p = new Program();
p.Process();
}
public void Process()
{
List<Boolean> flags = new List<Boolean>();
flags.Add(true);
flags.Add(false);
flags.Add(false);
flags.Add(true);
flags.Add(false);
bool b = true;
try
{
List<Boolean> trueFlags = flags
.Where<Boolean>(x => IsTrue(x, b))
.ToList();
Console.WriteLine(trueFlags.ToString());
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
public bool IsTrue(bool x, bool b)
{
return x ^ b;
}
I can't seem to find anything on this exception message on the internet.

Sitecore LINQ isn't really like normal LINQ to objects in .NET. Like LINQ to SQL isnt like normal LINQ.
What actually happens is that Sitecore parses the expression tree and "converts/translates" your conditions to a Search Query. By default this is to a Lucene query expression - but using a SearchProvider it could also translate to a SOLR expression or Coveo expression.
In the same way LINQ to SQL translates to a SQL expression.
You can see that the LINQ is actually an IQueryable and not IEnumerable.
When working on the IQuerable Sitecore must know how to translate it to the search expression. Sitecore doesn't know how to translate your properties and methods in the LINQ expression and that is why you get the error.
You should change your expression to only hold something that can be translated or create a predicate. You should look into the PredicateBuilder

Related

LINQ IQueryable: Set 1 Parameter to accept Where Clause

sample code
public class program
{
public void sample()
{
var qry = repo.All(); // returns IQueryable //came from EF
var keyword = "al";
var result = qry.DynamicFilter(keyword, filterconditions);
// it should return all persons with 'al' in their name and nickname
}
public List<Expression<Func<Person, string, bool>>> filterconditions
{
get
{
var ret = new List<Expression<Func<Person, string, bool>>>();
ret.Add((m,n) => m.Name.Contains(n));
ret.Add((m,n) => m.Nickname.Contains(n));
return ret;
}
}
}
this is my extension
public static class linqExt
{
public static IQueryable<T> DynamicFilter<T>(this IQueryable<T> qry, string keyword, IEnumerable<Expression<Func<T, string, bool>>> conditions) where: T
{
var ret = qry;
foreach(var item in conditions)
{
ret = ret.Where(m => item.Compile()(m, keyword));
}
return ret;
}
}
is there any way to implement this without using the Compile/Invoke??
ret = ret.Where(m=> func.Compile()(m, keyword));??
the problem is that I can't use the Compile because the collection is an IQueryable and i can't convert it to IEnumarable because if I do that it will pull all the data from database.
I am thinking if I can use the METHODS and PROPERTIES of the EXPRESSION class
any guest? thanks
We have done something similar; but we use the PredicateBuilder class presented here: https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/
var filter = PredicateBuilder.True<Person>()
.And(m => m.Name.Contains(n))
.And(m => m.Nickname.Contains(n));
Then you can just do qry.Where(filter) and it will apply your "dynamic" where clause. If this is LinqToEF this will cause the Where() clause to actually be generated and executed in SQL, which is probably want you want.
The PredicateBuilder is a very powerful class, and you can build some very elaborate clauses. When you need to do "ORs", be sure and "start" with a PredicateBuilder.False<Person>() statement.
You can also do conditional logic, and "append" statements to your filter, for example:
if (lookingForReallyLongNames == true)
{
filter = filter.And(m => m.Name.Length > 15);
}

Trying to save comma-separated list

Trying to save selections from a CheckBoxList as a comma-separated list (string) in DB (one or more choices selected). I am using a proxy in order to save as a string because otherwise I'd have to create separate tables in the DB for a relation - the work is not worth it for this simple scenario and I was hoping that I could just convert it to a string and avoid that.
The CheckBoxList uses an enum for it's choices:
public enum Selection
{
Selection1,
Selection2,
Selection3
}
Not to be convoluted, but I use [Display(Name="Choice 1")] and an extension class to display something friendly on the UI. Not sure if I can save that string instead of just the enum, although I think if I save as enum it's not a big deal for me to "display" the friendly string on UI on some confirmation page.
This is the "Record" class that saves a string in the DB:
public virtual string MyCheckBox { get; set; }
This is the "Proxy", which is some sample I found but not directly dealing with enum, and which uses IEnumerable<string> (or should it be IEnumerable<Selection>?):
public IEnumerable<string> MyCheckBox
{
get
{
if (String.IsNullOrWhiteSpace(Record.MyCheckBox)) return new string[] { };
return Record
.MyCheckBox
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(r => r.Trim())
.Where(r => !String.IsNullOrEmpty(r));
}
set
{
Record.MyCheckBox = value == null ? null : String.Join(",", value);
}
}
To save in the DB, I am trying to do this in a create class:
proxy.MyCheckBox = record.MyCheckBox; //getting error here
but am getting the error:
Cannot implicitly convert 'string' to System.Collections.Generic.IEnumerable'
I don't know, if it's possible or better, to use Parse or ToString from the API for enum values.
I know that doing something like this will store whatever I put in the ("") into the DB, so it's just a matter of figuring out how to overcome the error (or, if there is an alternative):
proxy.MyCheckBox = new[] {"foo", "bar"};
I am not good with this stuff and have just been digging and digging to come up with a solution. Any help is much appreciated.
You can accomplish this using a custom user type. The example below uses an ISet<string> on the class and stores the values as a delimited string.
[Serializable]
public class CommaDelimitedSet : IUserType
{
const string delimiter = ",";
#region IUserType Members
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y))
{
return true;
}
var xSet = x as ISet<string>;
var ySet = y as ISet<string>;
if (xSet == null || ySet == null)
{
return false;
}
// compare set contents
return xSet.Except(ySet).Count() == 0 && ySet.Except(xSet).Count() == 0;
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var outValue = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
if (string.IsNullOrEmpty(outValue))
{
return new HashSet<string>();
}
else
{
var splitArray = outValue.Split(new[] {Delimiter}, StringSplitOptions.RemoveEmptyEntries);
return new HashSet<string>(splitArray);
}
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var inValue = value as ISet<string>;
object setValue = inValue == null ? null : string.Join(Delimiter, inValue);
NHibernateUtil.String.NullSafeSet(cmd, setValue, index);
}
public object DeepCopy(object value)
{
// return new ISet so that Equals can work
// see http://www.mail-archive.com/nhusers#googlegroups.com/msg11054.html
var set = value as ISet<string>;
if (set == null)
{
return null;
}
return new HashSet<string>(set);
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public SqlType[] SqlTypes
{
get { return new[] {new SqlType(DbType.String)}; }
}
public Type ReturnedType
{
get { return typeof(ISet<string>); }
}
public bool IsMutable
{
get { return false; }
}
#endregion
}
Usage in mapping file:
Map(x => x.CheckboxValues.CustomType<CommaDelimitedSet>();

How do I apply a default IComparable<T> in a Linq OrderBy clause

I have a type which has a default sort order as it implements IComparable<T> and IComparable. I'm not getting the results I expect from LINQ , basically it looks as if the IComparable<T> which the type implements is not being applied.
I thought I would get the result I want with an expression in the form:
var result = MyEnumerable<T>.OrderBy(r => r);
where T itself implements IComparable<T>. It's not happening.
I can see related questions where specific IComparable<T> classes are specified for the sort, but I can't find one which uses the default IComparable<T> implemented by T itself.
My syntax is clearly incorrect. What is the correct syntax please?
Thanks in advance.
OrderBy uses the default comparer Comparer<T>.Default which in turn will default to use the IComparable<T> implementation for T, or the non-generic IComparable if the former does not exist.
This code works:
public class Program
{
static void Main(string[] args)
{
var list = new List<Stuff>
{
new Stuff("one"),
new Stuff("two"),
new Stuff("three"),
new Stuff("four")
};
var sorted = list.OrderBy(x => x);
foreach (var stuff in sorted)
{
Console.Out.WriteLine(stuff.Name);
}
}
}
public class Stuff : IComparable<Stuff>
{
public string Name { get; set; }
public Stuff(string name)
{
Name = name;
}
public int CompareTo(Stuff other)
{
return String.CompareOrdinal(Name, other.Name);
}
}
public static class GenericSorter
{
public static IOrderedEnumerable<T> Sort<T>(IEnumerable<T> toSort, Dictionary<string, SortingOrder> sortOptions)
{
IOrderedEnumerable<T> orderedList = null;
foreach (KeyValuePair<string, SortingOrder> entry in sortOptions)
{
if (orderedList != null)
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenBy");
}
else
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenByDescending");
}
}
else
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderBy");
}
else
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderByDescending");
}
}
}
return orderedList;
}
private static IOrderedEnumerable<T> ApplyOrder<T>(this IEnumerable<T> source, string property, string methodName)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression expr = param;
foreach (string prop in property.Split('.'))
{
expr = Expression.PropertyOrField(expr, prop);
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), expr.Type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, param);
MethodInfo mi = typeof(Enumerable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), expr.Type);
return (IOrderedEnumerable<T>)mi.Invoke(null, new object[] { source, lambda.Compile() });
}
}

Entity Framework: Mixed predicates of mixed entities

Sorry for my awful English.
I'm trying to extend my EF model (with LinqKit).
As long as I work with single entities expressions, I don't have any issue, but if I want to work with mixed expressions, I'm simply stuck.
For instance, a sort-of-post class like that...
partial class vit_post
{
//[...]
public static Expression<Func<vit_post, bool>> HasMetas(IEnumerable<MetaFilter> metaArgs)
{
var resultsInner = PredicateBuilder.True<vit_post>();
resultsInner.And(p=>p.vit_postmeta.Any(pm => (pm.hide == false)));
foreach (var metaArg in metaArgs)
{
var mf = metaArg;
resultsInner.And(p=>p.vit_postmeta.Any(pm => (pm.meta_key == mf.MetaKey && mf.Compare(pm.meta_value))));
if (mf.ChildrenMetaFilters != null)
{
Expression<Func<vit_post, bool>> resultsInner2;
switch (mf.LogicalOperator)
{
case LogicalOperators.AND:
resultsInner2 = PredicateBuilder.True<vit_post>();
resultsInner = resultsInner2.And(HasMetas(mf.ChildrenMetaFilters));
break;
case LogicalOperators.OR:
resultsInner2 = PredicateBuilder.False<vit_post>();
resultsInner = resultsInner2.Or(HasMetas(mf.ChildrenMetaFilters));
break;
}
}
}
return resultsInner;
}
//[...]
}
...extends an entity "vit_post" with an expression HasMetas.
Using this snippet, I get a subset of the entities, as expected:
public SearchResults GetResults()
{
var query = dbc.vit_posts.AsExpandable();
//[...]
if (SearchArgs.Metas != null)
{
var postsbycontent = vit_post.HasMetas(SearchArgs.Metas);
outer = Data.Utility.And(outer, postsbycontent);
}
//[...]
query = query.Where(outer);
var searchResults = new SearchResults
{
Items = searchResultsItems
};
return searchResults;
}
But if I try to move this this expression to the "vit_postmeta" entity like that:
partial class vit_postmeta
{
//[...]
var resultsInner = PredicateBuilder.True<vit_postmeta>();
resultsInner.And(pm => (pm.hide == false));
foreach (var metaArg in metaArgs)
{
var mf = metaArg;
resultsInner.And(pm => (pm.meta_key == mf.MetaKey && mf.Compare(pm.meta_value)));
if (mf.ChildrenMetaFilters != null)
{
Expression<Func<vit_postmeta, bool>> resultsInner2;
switch (mf.LogicalOperator)
{
case LogicalOperators.AND:
resultsInner2 = PredicateBuilder.True<vit_postmeta>();
resultsInner = resultsInner2.And(HasMetas(mf.ChildrenMetaFilters));
break;
case LogicalOperators.OR:
resultsInner2 = PredicateBuilder.False<vit_postmeta>();
resultsInner = resultsInner2.Or(HasMetas(mf.ChildrenMetaFilters));
break;
}
}
}
return resultsInner;
//[...]
}
My idea was to keep the original method in vit_post and change it like that:
partial class vit_post
{
//[...]
public static Expression<Func<vit_post, bool>> HasMetas(IEnumerable<MetaFilter> metaArgs)
{
var resultsInner = PredicateBuilder.True<vit_post>();
resultsInner.And(p=>p.vit_postmeta.HasMetas(metaArgs));
return resultsInner;
}
//[...]
}
But I can't do that, as "'vit_postmeta' does not contain a definition for 'HasMetas' and no extension method 'HasMetas' accepting a first argument of type 'vit_postmeta' could be found".
I'm missing something, I know, but I can't find what.
UPDATE
I've found an alternate solution (and perhaps the proper one too), implementing all my expression in respective entity partial classes.
For instance, vit_post had all expressions related to vit_posts's table, whilst vit_postmeta has all expressions related to vit_postmeta's table.
My Search class then has a private method for each entity. Something like:
private IQueryable<vit_post> QueryPosts()
{
IQueryable<vit_post> queryPosts = VITContext.vit_posts.AsExpandable();
Expression<Func<vit_post, bool>> outerPredicate = PredicateBuilder.True<vit_post>();
if (!_searchArgs.IncludeExpiredPosts)
{
Expression<Func<vit_post, bool>> postsByExpiration = vit_post.ExcludeExpired();
outerPredicate = Data.Utility.And(outerPredicate, postsByExpiration);
}
if (_searchArgs.LocaleCode != null)
{
Expression<Func<vit_post, bool>> postsByLanguage = vit_post.HasLocaleCode(_searchArgs.LocaleCode);
outerPredicate = Data.Utility.And(outerPredicate, postsByLanguage);
}
[...]
}
Then the GetResults() function calls all these methods and join them:
internal string GetResults()
{
IQueryable<vit_post> queryPosts = QueryPosts();
if (_searchArgs.Metas != null)
{
IEnumerable<vit_postmeta> queryMetas = QueryMetas();
queryPosts = from p in queryPosts
join m in queryMetas
on new {id = p.ID, loc = p.locale} equals new {id = m.post_id, loc = m.locale}
select p;
}
}
This way I can make the query works, but I'm still not sure this is the right way to do it.
You did not see/answer my question yet, but if vit_postmeta and vit_post are in any way related by inheritance you may be able to do something like:
public static Expression<Func<T, bool>> HasMetas<T>(IEnumerable<MetaFilter> metaArgs)
where T : vit_post
{
...
}
Assuming that vit_post is the super type.

Operator overloading in Linq queries

Operator overloading is working perfect in C# code, since I am trying in the following way.
**
public class HostCode
{
public string Code { get; set; }
public string Name { get; set; }
public static bool operator ==(HostCode hc1, HostCode hc2)
{
return hc1.Code == hc2.Code;
}
public static bool operator !=(HostCode hc1, HostCode hc2)
{
return true;
}
}**
I have a clas called HostCode and it contains 2 overloading methods (one for '==' and another for '!=')
And I created a collection of Host Codes below.
**var hostCodes = new List<HostCode>()
{
new HostCode(){ Code = "1", Name = "sreekanth" },
new HostCode(){ Code = "2", Name = "sajan" },
new HostCode(){ Code = "3", Name = "mathew" },
new HostCode(){ Code = "4", Name = "sachin" }
};**
***var hc = new HostCode() { Code = "1", Name = "sreekanth" };***
***var isEq = hostCodes[1] == hc;***
when I am trying like above, the respective operator method fired in HostCode class (in this case it is '=='). So that I can write my custom logic there.
But if Iam trying with a Linq query as below, it is not firing. But in this case also Iam comparing 2 objects having same type.
**var isEqual = from obj in hostCodes where (HostCode)obj == (HostCode)hc select obj;**
Can anyone please help me to find out a way which I can compare 2 objects by Linq queries?
You can use IEqualityComparer or override equals for this purpose.
public class HostCode
{
....
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj as HostCode == null ? false : (obj as HostCode).Code == Code;
}
}
And IEqualityComparer usage is when you have some equality on object and in some functions like Contain, ... you want use some specific equality:
public class EqualityComparer : IEqualityComparer<HostCode >
{
public bool Equals(HostCode x, HostCode y)
{
return y.ID == x.ID;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
Since your question is "how should I compare them for this to work" I would answer "you should overload and use .Equals() instead of =="
And what's with your overload definition of !=?

Resources