我想为网站建立一个advanced multiple parameters search
。
这是我的DBML和ORM模式:
用户可以在此advanced multiple parameters search
中搜索具有多个参数(例如大小,楼层,城市,价格等)的房地产。
这是我编写的处理此部分的功能。
private DataTable linq_search_by_details()
{
myDBMLDataContext ctx = new myDBMLDataContext(address);
var query = ctx.Estates.AsQueryable();
query = query.Where(c => c.eshId == int.Parse(ddlEshape.SelectedValue.ToString()));
query = query.Where(c => c.cityId == int.Parse(ddlcity.SelectedValue.ToString()));
query = query.Where(c => c.ETId == int.Parse(ddlType.SelectedValue.ToString()));
query = query.Where(c => c.dealingId == int.Parse(ddldeal.SelectedValue.ToString()));
query = query.Where(c => c.deedId == int.Parse(ddldeed.SelectedValue.ToString()));
if (!string.IsNullOrEmpty(txtPrepaymentFrom.Value.Trim()))
{
query = query.Where(c => int.Parse(c.prepayment) <= int.Parse(txtPrepaymentFrom.Value));
}
if (!string.IsNullOrEmpty(txtPrepaymentTo.Value.Trim()))
{
query = query.Where(c => int.Parse(c.prepayment) >= int.Parse(txtPrepaymentTo.Value));
}
if (!string.IsNullOrEmpty(txtPrepaymentFrom.Value.Trim()) && !string.IsNullOrEmpty(txtPrepaymentTo.Value.Trim()))
{
query = query.Where(c => int.Parse(c.prepayment) <= int.Parse(txtPrepaymentFrom.Value) && int.Parse(c.prepayment) >= int.Parse(txtPrepaymentTo.Value));
}
if (!string.IsNullOrEmpty(txtPriceFrom.Value.Trim()))
{
query = query.Where(c => int.Parse(c.price) <= int.Parse(txtPriceFrom.Value));
}
if (!string.IsNullOrEmpty(txtPriceTo.Value.Trim()))
{
query = query.Where(c => int.Parse(c.price) >= int.Parse(txtPriceTo.Value));
}
if (!string.IsNullOrEmpty(txtPriceFrom.Value.Trim()) && !string.IsNullOrEmpty(txtPriceTo.Value.Trim()))
{
query = query.Where(c => int.Parse(c.price) <= int.Parse(txtPriceFrom.Value) && int.Parse(c.price) >= int.Parse(txtPriceTo.Value));
}
if (!string.IsNullOrEmpty(txtFloor.Value.Trim()))
{
query = query.Where(c => c.eFloor == short.Parse(txtFloor.Value));
}
if (chbExchange.Checked)
{
query = query.Where(c => c.exchange == true);
}
var final = query.Select(c => new { c.esId,c.owId, c.City.cityName, c.EstateShape.eshName, c.EstateType.ETName, c.owner.owFname, c.owner.owLname, c.esSize, c.prepayment, c.price });
return Special.LINQResultToDataTable(final.ToList());
}
此功能运行良好,但现在我想更多地从EstateEquipment
和EstateFacility
中添加一些parameres。
如您在ORM
中所见,Estate
和EstateEquipment
之间的关系(也是Estate
和EstateFacility
)是one to many
。
现在我想让用户可以搜索cityId = 1
,size around 400m
中的遗产,例如,eqId = 1 and 2
中的EstateEquipment
和fId = 1 and 2
中的{来自EstateFacility
。
这就是我尝试处理最后一部分的方式。
foreach (ListItem item in cblEquipment.Items)
{
if (item.Selected)
{
eq = true;
}
}
if(eq)
{
var eqQuery = ctx.EstateEquipments.AsQueryable();
foreach (ListItem item in cblEquipment.Items)
{
if (item.Selected)
{
eqQuery = eqQuery.Where(c => c.eqId == int.Parse(item.Value.ToString()));
}
}
var eqFinal = eqQuery.Select(c => new { c.Estate.esId, c.Estate.owner.owId, c.Estate.City.cityName, c.Estate.EstateShape.eshName, c.Estate.EstateType.ETName, c.Estate.owner.owFname, c.Estate.owner.owLname, c.Estate.esSize, c.Estate.prepayment, c.Estate.price });
DataTable dtEq = Special.LINQResultToDataTable(eqFinal.ToList());
if(dtEq.Rows.Count>0)
{
this.Build_search(dtEq);
}
else
{
msg = "No record found";
sysMsg.Attributes["class"] = "";
sysMsg.Attributes["class"] = "alert alert-warning";
}
}
在这里,首先,我检查从列表中选择了哪种设备。然后根据选择的项目扩展查询。但是我不知道如何将这个新结果与最后一个结果甚至EstateFacility
结果结合起来。
感谢您的帮助。
答案 0 :(得分:3)
由于您的案子比较复杂,无法测试我的解决方案,我希望它至少可以为您指明正确的方向。
我会说尝试这样的事情:
if (cblEquipment.Items.Any(item => item.Selected))
{
var selectedEquipmentIds = cblEquipment.Items.Where(item => item.Selected).Select(item => int.Parse(item.Value.ToString()));
query = query.Where(c => ctx.EstateEquipments.Any(eq => eq.esId == c.esId && selectedEquipmentIds.Contains(eq.eqId)));
}
这出现在var final ...
之前的第一个代码段中。然后将不使用第二个片段,因此您在那里的错误处理也将需要移动...