2

私はインターネットを精査してきましたが、これまでに見つけたすべての例で、同じ種類のクエリを使用して検索を実行しています。これは、検索の Hello World バージョンだと思います。if ステートメントを使用していくつかのクエリから選択するものをいくつか見つけましたが、それでも真の力はありません。

(from c in context.table
where c.contains(searchField)).toList());

検索は非常に単純なので、下のスクリーンショットの検索クエリをどのように作成するか思いつきません。チェックボックス グループは句を作成する必要OR/INがあり、それ以外のほとんどすべてはAND

たとえば、ユーザーは をチェックSarasotaし、Venice築年数 1990 を入力し、ベッドルーム 1+ を選択し、販売日の範囲from1/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 クラシックを使用していた場合、ハードコードされた句といくつかのifsandforステートメントを組み合わせて使用​​し、クエリ文字列を自分で動的に構築します。

私は何らかの形で値を渡す何らかの種類の 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);
            }
4

1 に答える 1

8

LINQ ソース ( IQueryable<T>) はコンポーザブルなので、次のことができます。

IQueryable<Parcel> query = ctx.Parcels;
if(location != null)
    query = query.Where(x => x.Location == location);
if(tennisCourt)
    query = query.Where(x => x.HasTennisCourt);
if(minSaleDate != null)
    query = query.Where(x => x.SaleDate >= minSaleDate.Value);
if(maxSaleDate != null)
    query = query.Where(x => x.SaleDate <= maxSaleDate.Value);

最後に、次のことができます。

// first 50, plus a final one to display "more..." link
var page = query.Take(51).ToList();

また:

int count = query.Count();

秘訣は、完全に構成されたクエリのみを実行することです。つまり、クエリを列挙するもの ( foreachToListなど)、またはクエリを集約するもの ( 、 など) をCountすべてSum実行します。

于 2012-06-04T12:38:26.440 に答える