我有一點挑戰,對於mvc我很新。我的問題是,如何在c中編組foreach語句#
我如何分組一堆foreach語句,由它被調用的表的ID。這裏是我的代碼:
foreach (
var owner in
_entities.LLAttrDatas.Where(
n => n.AttrID == 2 && n.ValStr.Contains(searchInputModel.OwnersName)))
{
ViewBag.searchName = owner.ValStr;
}
foreach (
var ownerAddress in
_entities.LLAttrDatas.Where(
n => n.AttrID == 3 && n.ValStr.Contains(searchInputModel.OwnersAddress)))
{
ViewBag.searchAddress = ownerAddress.ValStr;
}
foreach (
var propertyAddress in
_entities.LLAttrDatas.Where(
n => n.AttrID == 4 && n.ValStr.Contains(searchInputModel.DescriptionOrLocationOfProp)))
{
ViewBag.searchProperty = propertyAddress.ValStr;
}
foreach (
var propertyVolumeNo in
_entities.LLAttrDatas.Where(
n => n.AttrID == 5 && n.ValStr.Contains(searchInputModel.VolumeNumber)))
{
ViewBag.searchPropertyVolumeNo = propertyVolumeNo.ValStr;
}
foreach (
var propertyPlanNo in
_entities.LLAttrDatas.Where(
n => n.AttrID == 6 && n.ValStr.Contains(searchInputModel.SurveyPlanNumber))
)
{
ViewBag.searchPropertyPlanNo = propertyPlanNo.ValStr;
}
現在,當用戶針對上述任何查詢進行搜索時,只顯示一個結果。我希望它顯示來自不同查詢的所有結果,因爲它們都具有相同的ID。任何幫助將不勝感激。
謝謝大家的寶貴意見和支持。我們每天學習。不管怎麼說,我發現這樣做的更好的辦法:
public IQueryable<DocumentProperty> GetDocuments(DocumentSearchInputModel searchInputModel)
{
Context = new DataContext();
if (Context != null)
{
var result = Context.DocumentsForSearch.AsQueryable();
if (searchInputModel != null)
{
result =
Context.DocumentsForSearch.Where(
d =>
d.DescriptionOrLocationOfProp.Contains(searchInputModel.DescriptionOrLocationOfProp)
|| d.OwnersAddress.Contains(searchInputModel.OwnersAddress)
|| d.OwnersName.Contains(searchInputModel.OwnersName)
|| d.SurveyPlanNumber.Contains(searchInputModel.SurveyPlanNumber)
|| d.VolumeNumber.Contains(searchInputModel.VolumeNumber)
|| d.FileUrl.Contains(searchInputModel.OwnersName));
}
Logger.Info($"Results found {result}");
return result;
}
return null;
}
現在我遇到的唯一問題是如何在視圖中顯示搜索結果。我如何做AttrID和ValStr匹配的foreach,並且如果用戶僅在一個字段中搜索(因爲它是一個多搜索系統),我希望結果顯示具有相同ID的所有字段(即d.ID)。謝謝。
我不知道你在努力完成什麼......但它的錯誤。如此非常錯誤。那些foreach循環應該是火星大小的紅旗給你。 – BradleyDotNET
你爲什麼這樣做?只需要一個'foreach'循環和一些'if' /'else if'語句。 – Kyle