ObjectQuery In Entity Framework 6 +

时间:2015-03-31 15:33:51

标签: c# asp.net linq entity-framework-6

我使用的是Entity Framework 4.0。并从数据库View进行过滤,如下所示。

 string query = string.Empty;

        if (OrganizationID != 0)
        {
            query = query + " it.OrganisationID=" + OrganizationID;
        }


        var classType = typeof(View_OrganizationAwardTypeMapping);

        var props = classType.GetProperties();

        List<ObjectParameter> param = new List<ObjectParameter>();

        query = Common.Instance.SearchQuery(SearchedQuery, query, props, out param);

        if (string.IsNullOrEmpty(query))
        {
            query = "  1=1";
        }


        ObjectQuery<View_OrganizationAwardTypeMapping> orgAwardType = _context.View_OrganizationAwardTypeMapping;//.Where(query, param.ToArray());

        if (IsAscending)
        {
            orgAwardType = orgAwardType.OrderBy("it." + ColumnName);
        }
        else
        {
            orgAwardType = orgAwardType.OrderBy("it." + ColumnName + " desc");
        }

我的SearchQuery方法是

public string SearchQuery(string SearchedQuery, string query, System.Reflection.PropertyInfo[] props, out List<ObjectParameter> param)
    {

        param = new List<ObjectParameter>();

        if (!string.IsNullOrEmpty(SearchedQuery) && SearchedQuery.Length > 0 && SearchedQuery != "Search...")
        {
            if (props.Length > 0)
            {
                query += " and ( ";


                int i = 0;

                foreach (var field in props)
                {
                    i++;

                    if (field.PropertyType.FullName.Contains("Int32"))
                    {
                        int temp = 0;
                        if (int.TryParse(SearchedQuery.Trim(), out temp))
                        {
                            query += ((" it." + field.Name + " =" + temp));
                            query += " OR ";
                        }
                    }
                    else if (field.PropertyType.FullName.Contains("Int64"))
                    {
                        long temp = 0;
                        if (long.TryParse(SearchedQuery.Trim(), out temp))
                        {
                            query += ((" it." + field.Name + " =" + temp));
                            query += " OR ";
                        }
                    }
                    else if (field.PropertyType.FullName.Contains("Decimal"))
                    {
                        decimal temp = 0;
                        if (decimal.TryParse(SearchedQuery.Trim(), out temp))
                        {
                            query += ((" it." + field.Name + " =" + temp));
                            query += " OR ";
                        }
                    }
                    else if (field.PropertyType.FullName.Contains("Double"))
                    {
                        double temp = 0;
                        if (double.TryParse(SearchedQuery.Trim(), out temp))
                        {
                            query += ((" it." + field.Name + " =" + temp));
                            query += " OR ";
                        }
                    }
                    else if (field.PropertyType.FullName.Contains("String"))
                    {
                        string temp = SearchedQuery.Trim();
                        query += ((" it." + field.Name + " like'%" + (temp.Contains("%") ? "[" : "") + temp.Replace("'", "''") + (temp.Contains("%") ? "]" : "") + "%'"));
                        query += " OR ";
                    }
                    else if (field.PropertyType.FullName.Contains("Boolean"))
                    {
                        string temp = SearchedQuery.Trim();
                        bool res;
                        if (Boolean.TryParse(temp, out res))
                        {
                            query += ((" it." + field.Name + " =" + res + ""));
                            query += " OR ";
                        }
                    }

                    else if (field.PropertyType.FullName.Contains("DateTime"))
                    {
                        string temp = SearchedQuery.Trim();
                        DateTime res;
                        if (DateTime.TryParse(temp, out res))
                        {
                            query += ((" ( it." + field.Name + " >@p" + i + ""));

                            query += " AND ";

                            ObjectParameter pr = new ObjectParameter("p" + i, res);

                            i++;

                            query += ((" it." + field.Name + " <@p" + i + " ) "));

                            ObjectParameter pr1 = new ObjectParameter("p" + i, res.AddDays(1));

                            param.Add(pr);
                            param.Add(pr1);


                            query += " OR ";
                        }
                    }
                    else if (field.PropertyType.FullName.Contains("TimeSpan"))
                    {
                        string temp = SearchedQuery.Trim();
                        TimeSpan res;
                        if (TimeSpan.TryParse(temp, out res))
                        {
                            if (res.Days == 0)
                            {
                                query += ((" it." + field.Name + " =@p" + i + ""));
                                query += " OR ";

                                ObjectParameter pr = new ObjectParameter("p" + i, res);

                                param.Add(pr);
                            }
                        }
                    }

                }

                query += " 1=0 ) ";

            }
        }
        return query;
    }

现在我将项目升级为Use Enity Framwork 6.1。此版本的EF不支持where的{​​{1}}条款。

有谁能告诉我如何使用EF6进行过滤。因为我在每次过滤时使用这种类型的代码,或者你可以说大约50个地方。我不想使用lamda表达式对每个页面进行更改。

有没有解决方法呢?

请为此建议任何解决方案

0 个答案:

没有答案