ASP.NET 动态 Linq 搜索

发布于 2024-10-06 20:02:35 字数 805 浏览 1 评论 0原文

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 query 中。

然后,如果有搜索数据,我会尝试使用 1 或 4 个可能的搜索来添加Where 子句。

帮助?


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)));
    }
}

First I query the DB and store in var query.

Then if there is search data I am trying to tack on the Where clause using 1 or 4 possible searches.

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一片旧的回忆 2024-10-13 20:02:35

我会使用 .Contains 代替。

I would use .Contains instead.

演出会有结束 2024-10-13 20:02:35

不要尝试使用 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", "[email protected]");
            table.Rows.Add(2, "Christoph", "Bar", "[email protected]");
            table.Rows.Add(3, "Michael", "FooBar", "[email protected]");
            table.Rows.Add(4, "Andreas", "BarFoo", "[email protected]");
            table.Rows.Add(5, "Carl", "Bar", "[email protected]");

            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

Don't try to imitate SQL-Behaviour with Linq. You got a list and can query this list based on object methods.

Try this instead:

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", "[email protected]");
            table.Rows.Add(2, "Christoph", "Bar", "[email protected]");
            table.Rows.Add(3, "Michael", "FooBar", "[email protected]");
            table.Rows.Add(4, "Andreas", "BarFoo", "[email protected]");
            table.Rows.Add(5, "Carl", "Bar", "[email protected]");

            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();
        }
    }
}

Output:

//Query ID
1
//Query Firstname
1
2
5
北座城市 2024-10-13 20:02:35

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

ETC

Just add to the query as you need to. You can chain multiple 'where' clauses onto it and they will execute in turn.

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);
    }

etc

避讳 2024-10-13 20:02:35

为什么不尝试一下表达式树

Why not give a shot with Expression Trees

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文