lambda方法中包含的超时过期问题

时间:2015-11-11 21:59:56

标签: c# sql asp.net-mvc-4 lambda entity-framework-6

我有这段代码:

 string[] splits = keyword.Split(' ');

        var fproducts = (from products in db.tbl_Product
                         where splits.Any(item => products.Prod_Name_Fa.Contains(item) ||
                    products.shortDesc.Contains(item) || products.Prod_Code.Contains(item))
                         select products).Distinct();

如果分裂有两个或三个单词,它可以工作,但是当我输入5个单词时,我会得到超时的错误,

在字段中搜索数组是否有其他解决方案?

更新

这是sql输出:

{SELECT 
    [Extent1].[ID] AS [ID], 
    [Extent1].[Register_Date] AS [Register_Date], 
    [Extent1].[tbl_VATID] AS [tbl_VATID], 
    [Extent1].[Prod_Name_Fa] AS [Prod_Name_Fa], 
    [Extent1].[Prod_Code] AS [Prod_Code], 
    [Extent1].[Prod_Description_Fa] AS [Prod_Description_Fa], 
    [Extent1].[Prod_Description_En] AS [Prod_Description_En], 
    [Extent1].[Prod_How_To_Use] AS [Prod_How_To_Use], 
    [Extent1].[Prod_Keywords] AS [Prod_Keywords], 
    [Extent1].[Prod_Model] AS [Prod_Model], 
    [Extent1].[User_Rating] AS [User_Rating], 
    [Extent1].[Admin_Rating] AS [Admin_Rating], 
    [Extent1].[Seen_Counter] AS [Seen_Counter], 
    [Extent1].[Is_Ashantyvn_Salable] AS [Is_Ashantyvn_Salable], 
    [Extent1].[shortDesc] AS [shortDesc], 
    [Extent1].[basePrice] AS [basePrice], 
    [Extent1].[Discount] AS [Discount], 
    [Extent1].[discountDateFrom] AS [discountDateFrom], 
    [Extent1].[discountDateTo] AS [discountDateTo], 
    [Extent1].[Sex] AS [Sex], 
    [Extent1].[Publish] AS [Publish], 
    [Extent1].[basePic] AS [basePic], 
    [Extent1].[suggestDate] AS [suggestDate], 
    [Extent1].[isSuggest] AS [isSuggest], 
    [Extent1].[homesuggestDate] AS [homesuggestDate], 
    [Extent1].[homeisSuggest] AS [homeisSuggest], 
    [Extent1].[suggestNum] AS [suggestNum], 
    [Extent1].[tagOrder] AS [tagOrder]
    FROM [dbo].[tbl_Product] AS [Extent1]
    WHERE  EXISTS (SELECT 
        1 AS [C1]
        FROM  (SELECT 
            N'رژ' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
        UNION ALL
            SELECT 
            N'لب' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]
        UNION ALL
            SELECT 
            N'دو' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable3]
        UNION ALL
            SELECT 
            N'رنگ' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable4]
        UNION ALL
            SELECT 
            N'لوویو' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable5]) AS [UnionAll4]
        WHERE (( CAST(CHARINDEX([UnionAll4].[C1], [Extent1].[Prod_Name_Fa]) AS int)) > 0) OR (( CAST(CHARINDEX([UnionAll4].[C1], [Extent1].[shortDesc]) AS int)) > 0) OR (( CAST(CHARINDEX([UnionAll4].[C1], [Extent1].[Prod_Code]) AS int)) > 0)
    )}

2 个答案:

答案 0 :(得分:1)

我不确定实体框架使用什么机制来翻译您的查询,但是您可能会得到一些不必要的复杂内容。尝试编写自己的查询交集,返回每个关键字的结果,如下所示:

string[] splits = keyword.Split(' ');
if(!splits.Any())
    return db.tbl_Product; // or something
var queries = splits
    .Select(item => db.tbl_Product
        .Where(product => product.Prod_Name_Fa.Contains(item)  ||
                product.shortDesc.Contains(item) || 
                product.Prod_Code.Contains(item))));
return items.Aggregate((q1, q2) => q1.Union(q2));

答案 1 :(得分:0)

为了获得最佳性能,我必须选择模型的ID而不是全部, 它应该是这样的:

 string[] splits = keyword.Split(' ');

        var fproducts = (from products in db.tbl_Product
                         where splits.Any(item => products.Prod_Name_Fa.Contains(item) ||
                    products.shortDesc.Contains(item) || products.Prod_Code.Contains(item))
                         select products.ID).Distinct();

<强> products.ID

然后在等于这些ID时选择记录。