私はインターネットを精査してきましたが、これまでに見つけたすべての例で、同じ種類のクエリを使用して検索を実行しています。これは、検索の Hello World バージョンだと思います。if ステートメントを使用していくつかのクエリから選択するものをいくつか見つけましたが、それでも真の力はありません。
(from c in context.table
where c.contains(searchField)).toList());
検索は非常に単純なので、下のスクリーンショットの検索クエリをどのように作成するか思いつきません。チェックボックス グループは句を作成する必要OR/IN
があり、それ以外のほとんどすべてはAND
たとえば、ユーザーは をチェックSarasota
し、Venice
築年数 1990 を入力し、ベッドルーム 1+ を選択し、販売日の範囲from
1/1/2000 を入力して、フィールドを空白のままにしto
ます。
プレーンな古い SQL では、これが当てはまります。
where city = 'sarasota' or city = 'Venice' AND yearbuilt = 1990 AND bedrooms >= 1 AND fromSaleDate >= 1/1/2000 AND toSaleDate = 12/31/9999
古い学校の sql 文字列と ASP クラシックを使用していた場合、ハードコードされた句といくつかのifs
andfor
ステートメントを組み合わせて使用し、クエリ文字列を自分で動的に構築します。
私は何らかの形で値を渡す何らかの種類の searchViewModel (まだ書かれていない) を想定しています。そのため、その ViewModel のコード サンプルを含めることはできません。そして本当のキッカーはプロパティ機能セクションです。これは、使用可能なプロパティ機能の動的なリストになるため、クエリの一部としてハードコーディングすることはできません。
どういうわけかこれは関連しているかもしれないと思いますが、よくわかりません。LINQ を使用してリストをフィルタリングする
正しい方向への一歩を踏み出すのを手伝ってください。ありがとう
アップデート
プロファイラーで実行されたクエリを次に示します。インタラクティブに実行できるように変数を宣言するように微調整しましたが、クエリ自体は変更されていません
declare @p__linq__0 as datetime
declare @p__linq__1 as datetime
set @p__linq__0='1989-01-01 00:00:00'
set @p__linq__1='1990-01-01 00:00:00'
SELECT TOP (10)
[Project3].[STRAP] AS [STRAP],
[Project3].[SITUS] AS [SITUS],
[Project3].[MAILING_ADDRESS] AS [MAILING_ADDRESS],
[Project3].[PROPERTY_USE] AS [PROPERTY_USE],
[Project3].[SUBDIVISION] AS [SUBDIVISION],
[Project3].[DSCR] AS [DSCR],
[Project3].[LAND_AREA] AS [LAND_AREA],
[Project3].[INCORPORATION] AS [INCORPORATION],
[Project3].[SEC_TWP_RGE] AS [SEC_TWP_RGE],
[Project3].[CENSUS] AS [CENSUS]
FROM ( SELECT [Project3].[STRAP] AS [STRAP], [Project3].[SITUS] AS [SITUS], [Project3].[MAILING_ADDRESS] AS [MAILING_ADDRESS], [Project3].[PROPERTY_USE] AS [PROPERTY_USE], [Project3].[SUBDIVISION] AS [SUBDIVISION], [Project3].[DSCR] AS [DSCR], [Project3].[LAND_AREA] AS [LAND_AREA], [Project3].[INCORPORATION] AS [INCORPORATION], [Project3].[SEC_TWP_RGE] AS [SEC_TWP_RGE], [Project3].[CENSUS] AS [CENSUS], row_number() OVER (ORDER BY [Project3].[STRAP] ASC) AS [row_number]
FROM ( SELECT
[Distinct2].[STRAP] AS [STRAP],
[Distinct2].[SITUS] AS [SITUS],
[Distinct2].[MAILING_ADDRESS] AS [MAILING_ADDRESS],
[Distinct2].[PROPERTY_USE] AS [PROPERTY_USE],
[Distinct2].[SUBDIVISION] AS [SUBDIVISION],
[Distinct2].[DSCR] AS [DSCR],
[Distinct2].[LAND_AREA] AS [LAND_AREA],
[Distinct2].[INCORPORATION] AS [INCORPORATION],
[Distinct2].[SEC_TWP_RGE] AS [SEC_TWP_RGE],
[Distinct2].[CENSUS] AS [CENSUS]
FROM ( SELECT DISTINCT
[Distinct1].[STRAP] AS [STRAP],
[Distinct1].[SITUS] AS [SITUS],
[Distinct1].[MAILING_ADDRESS] AS [MAILING_ADDRESS],
[Distinct1].[PROPERTY_USE] AS [PROPERTY_USE],
[Distinct1].[SUBDIVISION] AS [SUBDIVISION],
[Distinct1].[DSCR] AS [DSCR],
[Distinct1].[LAND_AREA] AS [LAND_AREA],
[Distinct1].[INCORPORATION] AS [INCORPORATION],
[Distinct1].[SEC_TWP_RGE] AS [SEC_TWP_RGE],
[Distinct1].[CENSUS] AS [CENSUS]
FROM (SELECT DISTINCT
[Extent1].[STRAP] AS [STRAP],
[Extent1].[SITUS] AS [SITUS],
[Extent1].[MAILING_ADDRESS] AS [MAILING_ADDRESS],
[Extent1].[PROPERTY_USE] AS [PROPERTY_USE],
[Extent1].[SUBDIVISION] AS [SUBDIVISION],
[Extent1].[DSCR] AS [DSCR],
[Extent1].[LAND_AREA] AS [LAND_AREA],
[Extent1].[INCORPORATION] AS [INCORPORATION],
[Extent1].[SEC_TWP_RGE] AS [SEC_TWP_RGE],
[Extent1].[CENSUS] AS [CENSUS]
FROM [dbo].[Properties] AS [Extent1]
INNER JOIN [dbo].[Transfers] AS [Extent2] ON [Extent1].[STRAP] = [Extent2].[STRAP]
WHERE CAST( [Extent2].[TRANS_DATE] AS datetime2) >= @p__linq__0 ) AS [Distinct1]
INNER JOIN [dbo].[Transfers] AS [Extent3] ON [Distinct1].[STRAP] = [Extent3].[STRAP]
WHERE CAST( [Extent3].[TRANS_DATE] AS datetime2) <= @p__linq__1
) AS [Distinct2]
) AS [Project3]
) AS [Project3]
WHERE [Project3].[row_number] > 0
アップデート
if (Model == null)
{
Model = new SearchViewModel();
}
TryUpdateModel(Model);
Model.Locations = _service.GetLocations().Select(x => new SelectListItem { Text = x.Value, Value = x.Key }).ToList();
var results = _service.GetProperties();
if (Model.SelectedLocation != null)
results = (from r in results
where r.SITUS.ToLower().Contains(Model.SelectedLocation.ToLower())
select r);
if (Model.ParcelID != null)
results = (from r in results
where r.STRAP.StartsWith(Model.ParcelID.Replace("-", "")) || r.STRAP.Equals(Model.ParcelID.Replace("-", ""))
select r);
if (Model.SqftFrom != null)
results = (from r in results
from b in r.Buildings
where b.GROSS_AREA >= Model.SqftFrom
select r).Distinct();
if (Model.SqftTo != null)
results = (from r in results
from b in r.Buildings
where b.GROSS_AREA <= Model.SqftTo
select r).Distinct();
if (Model.Bedrooms != null)
results = (from r in results
from b in r.Buildings
from s in b.StructuralElements
where s.VALUE == Model.Bedrooms && s.DSCR.ToLower() == "bedrooms"
select r).Distinct();
if (Model.Bathrooms != null)
results = (from r in results
from b in r.Buildings
from s in b.StructuralElements
where s.VALUE == Model.Bathrooms && s.CATEGORY == "BT"
select r).Distinct();
if (Model.SaleAmountFrom != null)
results = (from r in results
from t in r.Transfers
where t.AMOUNT >= Model.SaleAmountFrom
select r).Distinct();
if (Model.SaleAmountTo != null)
results = (from r in results
from t in r.Transfers
where t.AMOUNT <= Model.SaleAmountTo
select r).Distinct();
if (Model.SalesFrom != null)
results = (from r in results
from t in r.Transfers
where t.TRANS_DATE >= Model.SalesFrom
select r).Distinct();
if (Model.SalesTo != null)
results = (from r in results
from t in r.Transfers
where t.TRANS_DATE <= Model.SalesTo
select r).Distinct();
//if (Model.SalesTo != null)
// results = (from r in results
// from t in r.Transfers
// where t.TRANS_DATE <= Model.SalesTo
// select r).Distinct();
results = results.OrderBy(x => x.STRAP);
Model.SearchResults = results.ToPagedList(page ??1 , size ?? 10);
if (Model.SearchResults.Count() == 1)
{
var strap = Model.SearchResults.FirstOrDefault().STRAP;
return RedirectToRoute("Details", new { id = strap });
}
else
{
return View(Model);
}