I have a class with IDictionary<string,string> property. I need to
search objects of that class by indexes and values in that dictionary.
For example it may be a Product class with a dictionary of localized
names where dictionary index is a culture name and then I need to
search for products by their name for specified culture.
Localization techniques its not an issue here. Its just a sample.
public class Product
{
public Product()
{
Names = new Dictionary<string, string>();
}
public virtual Guid ID { get; protected set; }
public virtual IDictionary<string, string> Names { get;
protected set; }
public virtual string Name
{
get
{
string name = null;
Names.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out name);
return name;
}
set
{
Names[Thread.CurrentThread.CurrentCulture.Name] =
value;
}
}
}
public class ProducMapping : ClassMapping<Product>
{
public ProducMapping()
{
Table("Products");
Id<Guid>(p => p.ID, map =>
map.Generator(Generators.Guid));
//<map name="Names" table="ProductNames">
// <key column="ProductId"/>
// <index column="CultureName" type="System.String"/>
// <element column="ProductName" type="System.String"/>
//</map>
Map<string, string>(p => p.Names,
collectionMapping =>
{
collectionMapping.Table("ProductNames");
collectionMapping.Key(m => m.Column("ProductId"));
collectionMapping.Cascade(Cascade.All);
},
indexMapping => indexMapping.Element(m =>
m.Column("CultureName")),
valueMapping => valueMapping.Element(m =>
m.Column("ProductName")));
}
}
How to implement a query that whold result into the following sql?
SELECT *
FROM ProductNames
LEFT OUTER JOIN Products ON ProductNames.ProductId = Products.ID
WHERE (ProductNames.CultureName = :culture) AND
(ProductNames.ProductName LIKE :name)
I have tried:
var res = session.Query<Product>()
.Where(p => p.Names.Any(kvp => kvp.Key == culture &&
kvp.Value.Contains(name)))
.ToList();
But it throws NHibernate.QueryException:
cannot dereference scalar collection element: Key
[.Where[TestLocalization.Product]
(NHibernate.Linq.NhQueryable`1[TestLocalization.Product], Quote((p, )
=> (.Any[System.Collections.Generic.KeyValuePair`2[[System.String,
mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]]
(p.Names, (kvp, ) => (AndAlso(String.op_Equality(kvp.Key, p1),
kvp.Value.Contains(p2, ))), ))), )]
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.