【问题标题】:ASP.NET Dynamic Linq SearchASP.NET 动态 Linq 搜索
【发布时间】:2011-05-22 22:21:48
【问题描述】:
var query = (from u in results 选择 u).AsQueryable(); //构建where子句 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 子句。

帮助?

【问题讨论】:

    标签: asp.net linq dynamic like-keyword


    【解决方案1】:

    我会改用 .Contains。

    【讨论】:

    • 他只想要以用户值开头的名称。
    【解决方案2】:

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

    试试这个:

    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
    

    【讨论】:

    • 我用一个更完整的例子编辑了我的答案。你应该得到这条线。始终使用标准方法,而不是任何 sql 方法。因此,对于您上面示例中的电子邮件,.contains、.equals 您比较对象的位置、startswith 等等。顺便说一句,查询的结果是可查询的,所以不需要转换。
    • 很好,现在orderby子句怎么样了?
    • var sortColumnBy = userRequest.sortColumn; var orderedQuery = query.OrderBy(x => x.AttributeBag[ sortColumnBy ] + " " + userRequest.sortType)
    【解决方案3】:

    只需根据需要添加到查询中。您可以将多个“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);
        }
    

    【讨论】:

      【解决方案4】:

      为什么不试试Expression Trees

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多