ASP.NET动态Linq搜索

时间:2010-12-09 15:11:27

标签: asp.net linq dynamic like-keyword


var query = (from u in results
            select u).AsQueryable();


//Build where clause
if (!string.IsNullOrEmpty(userRequest.searchData))
{
    if (userRequest.searchBy == "LastName")
    {
        var likestr = userRequest.searchData.Trim();
        query = (from n in query where n.StartsWith(likestr) select n).AsQueryable();

    }
    if (userRequest.searchBy == "FirstName")
    {

    }
    if (userRequest.searchBy == "Email")
    {
        //var likestr = string.Format("%{0}%", userRequest.searchData.Trim());

    }
    if (userRequest.searchBy == "UserId")
    {
        query = query.Where(x => SqlMethods.Equals(x.UserId, Convert.ToInt32(userRequest.searchData)));
    }
}

首先,我查询数据库并存储在var查询中。

然后,如果有搜索数据,我试图使用1或4个可能的搜索来处理Where子句。

帮助?

4 个答案:

答案 0 :(得分:0)

我会改用.Contains。

答案 1 :(得分:0)

不要试图用Linq模仿SQL-Behavior。你有一个列表,可以根据对象方法查询这个列表。

请改为尝试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("FIRSTNAME", typeof(string));
            table.Columns.Add("LASTNAME", typeof(string));
            table.Columns.Add("EMAIL", typeof(string));

            // Here we add five DataRows.
            table.Rows.Add(1, "Chris", "Foo", "chris.foo@mail.com");
            table.Rows.Add(2, "Christoph", "Bar", "christoph.bar@mail.com");
            table.Rows.Add(3, "Michael", "FooBar", "michael.foobar@mail.com");
            table.Rows.Add(4, "Andreas", "BarFoo", "andreas.barfoo@mail.com");
            table.Rows.Add(5, "Carl", "Bar", "carl.bar@mail.com");

            Console.WriteLine("//Query ID");
            var query1 = (from dr in table.AsEnumerable() where dr.Field<int>("ID") == 1 select dr).FirstOrDefault();

            Console.WriteLine(query1.Field<int>("ID"));

            Console.WriteLine("//Query Firstname");
            var query2 = (from dr in table.AsEnumerable() where dr.Field<string>("FIRSTNAME").StartsWith("C") select dr).ToList<System.Data.DataRow>();

            foreach (var q in query2)
            {
                Console.WriteLine(q.Field<int>("ID"));
            }

            Console.ReadLine();
        }
    }
}

输出:

//Query ID
1
//Query Firstname
1
2
5

答案 2 :(得分:0)

只需根据需要添加到查询中即可。您可以将多个“where”子句链接到它上,然后它们将依次执行。

var query = (from u in results select u);

if (!string.IsNullOrEmpty(userRequest.searchData))
{
    if (userRequest.searchBy == "LastName")
    {
        var likestr = userRequest.searchData.Trim();
        query = (from n in query where n.LastName.StartsWith(likestr) select n);
    }
    if (userRequest.searchBy == "UserId")
    {
        var userId = Convert.ToInt32(userRequest.searchData);
        query = (from n in query where n.UserId == userId select n);
    }

答案 3 :(得分:0)

为什么不用Expression Trees

拍摄