Can anybody see an issue with the following query?
from prod in Product.All() join desc in ProductDescription.All() on new { pid = prod.ProductId, cc = _currentCultureCode } equals new { pid = desc.ProductId, cc = desc.CultureCode } where desc.DisplayName.StartsWith(!string.IsNullOrEmpty(Param1) ? Param1 : desc.DisplayName) && ( from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Medium" && prop.Value == ((!string.IsNullOrEmpty(Param2) && Param2 != "Any") ? Param2 : prop.Value) select prop.ProductId ).Contains(prod.ProductId) && ( from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Format" && prop.Value == ((!string.IsNullOrEmpty(Param3) && Param3 != "Any") ? Param3 : prop.Value) select prop.ProductId ).Contains(prod.ProductId) && ( from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Presentation" && prop.Value == ((!string.IsNullOrEmpty(Param4) && Param4 != "Any") ? Param4 : prop.Value) select prop.ProductId ).Contains(prod.ProductId) select prod;
It currently throws an exception
Sequence contains more than one matching element
I'm trying to pull back products with certain properies, ie Products with a "Medium" property of X AND a "Format" property of Y AND a "Presentation" property Z
Well, it's not pretty, but it's the only way I could get it to work:
var q3 = (from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Medium" && prop.Value == ((!string.IsNullOrEmpty(Param2) && Param2 != "Any") ? Param2 : prop.Value) select prop.ProductId).ToList();
q3.Add(-1); // Prevents NHibernate from throwing an exception if list is empty
var q4 = (from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Format" && prop.Value == ((!string.IsNullOrEmpty(Param3) && Param3 != "Any") ? Param3 : prop.Value) select prop.ProductId).ToList();
q4.Add(-1); // Prevents NHibernate from throwing an exception if list is empty
var q5 = (from prop in ProductProperty.All() join field in ProductDefinitionField.All() on prop.ProductDefinitionFieldId equals field.ProductDefinitionFieldId where field.Name == "Presentation" && prop.Value == ((!string.IsNullOrEmpty(Param4) && Param4 != "Any") ? Param4 : prop.Value) select prop.ProductId).ToList();
q5.Add(-1); // Prevents NHibernate from throwing an exception if list is empty
var q6 = from prod in Product.All() join desc in ProductDescription.All() on new { pid = prod.ProductId, cc = _currentCultureCode } equals new { pid = desc.ProductId, cc = desc.CultureCode } where desc.DisplayName.StartsWith(!string.IsNullOrEmpty(Param1) ? Param1 : desc.DisplayName) && q3.Contains(prod.ProductId) && q4.Contains(prod.ProductId) && q5.Contains(prod.ProductId) && prod.DisplayOnSite == true select prod;
Finally got some time to sit down and write a little bit of code. If I'm not mistaken I believe you can achieve the same thing with the following LINQ statement:
var q = from product in Product.All()
where product.ProductProperties.Where(property =>
(property.ProductDefinitionField.Name == "MyProperty" && property.Value == "MyPropertyValue")
|| (property.ProductDefinitionField.Name == "MyOtherProperty" && property.Value == "MyotherPropertyValue")).Count() == 2
&& product.ParentProductId == null
select product;
Querying products
Hi Guys,
Can anybody see an issue with the following query?
It currently throws an exception
Sequence contains more than one matching element
I'm trying to pull back products with certain properies, ie Products with a "Medium" property of X AND a "Format" property of Y AND a "Presentation" property Z
Well, it's not pretty, but it's the only way I could get it to work:
Finally got some time to sit down and write a little bit of code. If I'm not mistaken I believe you can achieve the same thing with the following LINQ statement:
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.