对通用列表执行操作

发布于 2024-11-09 02:15:12 字数 1238 浏览 0 评论 0原文

我不知道我所问的是否可能。这就是我来这里讨论的原因。

我有一个自定义列表,其中包含所有用户的信息及其详细信息。

在我的应用程序启动时,我从数据库填充它。换句话说,它包含数据库的副本。因此,我不想每次都从数据库中提取数据,而是想在此列表上执行操作。

我编写了一个从数据库表中搜索用户的查询,我想将其转换为该列表,以便与数据库相关的处理最小化。这是单个设备应用程序,没有其他用户修改该应用程序。

List<Users> selectedUsers = new List<Users>();
sql_cmd = new SqlCommand();
sql_cmd.Connection = sql_con;
#region First Name Search
if (category == SearchCategory.ByFirstName)
{
    sql_cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = Value;
    sql_cmd.CommandText = "SELECT * FROM AccountsUsers  WHERE accFirstName Like @firstName+'%'";
    SqlDataReader reader = sql_cmd.ExecuteReader();
    if (reader.HasRows == true)
    {
        while (reader.Read())
        {
            Users userToAdd = new Users();
            userToAdd.userId = int.Parse(reader["Id"].ToString());
            userToAdd.firstName = reader["accFirstName"].ToString();
            userToAdd.lastName = reader["accLastName"].ToString();
            //  GetAccountsDetails(userToAdd.userId, ref userToAdd);
            selectedUsers.Add(userToAdd);
        }
    }
}

我想做的是搜索我制作的列表,而不是用户的数据库。例如列表包含许多用户,如果在搜索框中输入AS,那么我可以过滤掉所有条目从AS开始。 想要与查询通配符类似的功能。

I dont know if what i am asking is possible or not. That is why I am here to discuss .

I have a custom List that contains the information of all the users and their details.

On my application start I populate it from database. In other words it contains the copy of database. So instead of pulling data everytime from the database I want to perform operation on this list.

I wrote a query that searches users from database table and I want to translate it for this list so that my processing related to database is minimum. This is a single device application and no other user modifies the application.

List<Users> selectedUsers = new List<Users>();
sql_cmd = new SqlCommand();
sql_cmd.Connection = sql_con;
#region First Name Search
if (category == SearchCategory.ByFirstName)
{
    sql_cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = Value;
    sql_cmd.CommandText = "SELECT * FROM AccountsUsers  WHERE accFirstName Like @firstName+'%'";
    SqlDataReader reader = sql_cmd.ExecuteReader();
    if (reader.HasRows == true)
    {
        while (reader.Read())
        {
            Users userToAdd = new Users();
            userToAdd.userId = int.Parse(reader["Id"].ToString());
            userToAdd.firstName = reader["accFirstName"].ToString();
            userToAdd.lastName = reader["accLastName"].ToString();
            //  GetAccountsDetails(userToAdd.userId, ref userToAdd);
            selectedUsers.Add(userToAdd);
        }
    }
}

What i indend to do is search list that i have made not the database for the users.for example List contains many users and if in search box i enter ASthen I can filter out all entries Starting with AS.
Want similar functionality that query wild card does.

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

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

发布评论

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

评论(1

°如果伤别离去 2024-11-16 02:15:12

很模糊的问题。你的问题到底是什么?当然,可以在程序中执行所有操作,并仅在需要或应用程序退出时将数据发送到数据库。有几种方法可以做到这一点。

你开始的方式没问题。您可以以任何您想要的方式更改用户,最后再次运行所有用户(或仅运行那些已更改的用户)并编写一个方法将它们全部更新到数据库。

[编辑]

看到你的评论我假设你在谈论 linq。

使用 Linq (System.Linq),您可以在列表上更改、排序、跳过或获取......等。

当您想要应用排序/排序时,您可以使用类似的东西

selectedList.Where(m=>m.StartsWith("AS")); 
selectedList.OrderBy(m=>m.firstname);

,最好不要将 selectedList 设置为此,因为您想继续使用它。

所以无论你设置什么数据源。您可以将其 itemssource 或 datacontext 设置为上述任意一个。

Pretty vague question. What is exactly your question? It is ofcourse possible to do everything in the program and send data to the database only on demand or application exit. There are several ways to do this.

The way you start is okay. You can alter users in any way you want and in the end you run throu them all again (or only those that have been change) and write a method to update them all to the database again.

[Edit]

Seeing your comment I assume you are talking about linq.

With Linq (System.Linq) you can alter, order, skip or take.. and such on lists.

When / where you want to apply ordering / sorting you can use stuff like

selectedList.Where(m=>m.StartsWith("AS")); 
selectedList.OrderBy(m=>m.firstname);

It's best not to set the selectedList to this since you want to keep using it.

So to whatever datasource you set it. You can set it's itemssource or datacontext to either of those above.

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