Hello,
I'm trying to figure out a way to group, sort, and remove objects returned from the database using criteria expressions. What I have currently works fine, but i need the query more refined. Below gets units from the DB based on the criteria settings. What I need to add to this is : if the returned units have the same buildingID check to see if units types and Dates are the same, if yes select unit with lowest price and remove the rest under that building ID.
Here's the kicker, the list returned by this query can have multiple building id's. So can I achieve the above with NHibernate query's? If no do you have any ideas on how to help me?
Let me know if you need more clarification.
Code:
public static IList<MFISHMAN.WEB.DATA.Entities.Unit> getUnitSearchResults(string Price, string Type, string Date)
{
NHibernate.ISession currentSession = MFISHMAN.WEB.DATA.MfishmanApplication.GetCurrentSession();
NHibernate.ICriteria criteria = currentSession.CreateCriteria(typeof(MFISHMAN.WEB.DATA.Entities.Unit));
int iType = Convert.ToInt32(Type);
int iDate = Convert.ToInt32(Date);
string uDate = "";
string uAvail = Date;
string uPrice = Price;
string[] strSplitArr = uPrice.Split(new Char[] { ',' });
if (Date != "")
{
criteria.Add(NHibernate.Expression.Expression.Like("Date", iDate));
}
if (Price != "")
{
criteria.Add(NHibernate.Expression.Expression.Between("Price", Convert.ToString(strSplitArr[0]), Convert.ToString(strSplitArr[1])));
}
if (Type != "")
{
criteria.Add(NHibernate.Expression.Expression.Like("Type.TypeID", iType));
}
if (Date == "" && Price == "" && Type == "")
{
}
criteria.CreateCriteria("Type").AddOrder(NHibernate.Expression.Order.Asc("SortPriority"));
criteria.AddOrder(NHibernate.Expression.Order.Asc("Price"));
IList<MFISHMAN.WEB.DATA.Entities.Unit> units = criteria.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity).List<MFISHMAN.WEB.DATA.Entities.Unit>();
return units;