如何保存从查询中检索到的数据
我之前问过这个问题并得到了答案 编写查询的最佳方法,但问题是,如果您必须将此结果保存在列表中,则会出现重复的记录。例如 给定连接的结果表 示例
看到有重复的行。如何过滤掉它们,同时保存订单号数据? 当然可能有一些方法,但我正在寻找一些好方法,
我们如何将数据存储在列表中而不在列表中创建重复的行?
我当前的表代码是
int lastUserId = 0;
sql_cmd = new SqlCommand();
sql_cmd.Connection = sql_con;
sql_cmd.CommandText = "SELECT * FROM AccountsUsers LEFT JOIN Accounts ON AccountsUsers.Id = Accounts.userId ORDER BY AccountsUsers.accFirstName";
SqlDataReader reader = sql_cmd.ExecuteReader();
if (reader.HasRows == true)
{
Users userToAdd = new Users();
while (reader.Read())
{
userToAdd = new Users();
userToAdd.userId = int.Parse(reader["Id"].ToString());
userToAdd.firstName = reader["accFirstName"].ToString();
userToAdd.lastName = reader["accLastName"].ToString();
lastUserId = userToAdd.userId;
Websites domainData = new Websites();
domainData.domainName = reader["accDomainName"].ToString();
domainData.userName = reader["accUserName"].ToString();
domainData.password = reader["accPass"].ToString();
domainData.URL = reader["accDomain"].ToString();
userToAdd.DomainData.Add(domainData);
allUsers.Add(userToAdd);
}
}
对于第二个表,我有自定义列表,它将保存第二个表中所有数据的条目。
返回的表是具有联接的表,并且具有相同的多行
I previously asked the question and got answer to Best approach to write query but the problem is that if you have to save this result in a list then there duplication of records. For example
the resultant table of the join given EXAMPLE
See there are duplicate rows. How can you filter them out, and yet save the data of order number?
Of course there may be some ways but I am looking for some great ways
How can we store the data in list and not create duplicate rows in list?
My current code for my tables is
int lastUserId = 0;
sql_cmd = new SqlCommand();
sql_cmd.Connection = sql_con;
sql_cmd.CommandText = "SELECT * FROM AccountsUsers LEFT JOIN Accounts ON AccountsUsers.Id = Accounts.userId ORDER BY AccountsUsers.accFirstName";
SqlDataReader reader = sql_cmd.ExecuteReader();
if (reader.HasRows == true)
{
Users userToAdd = new Users();
while (reader.Read())
{
userToAdd = new Users();
userToAdd.userId = int.Parse(reader["Id"].ToString());
userToAdd.firstName = reader["accFirstName"].ToString();
userToAdd.lastName = reader["accLastName"].ToString();
lastUserId = userToAdd.userId;
Websites domainData = new Websites();
domainData.domainName = reader["accDomainName"].ToString();
domainData.userName = reader["accUserName"].ToString();
domainData.password = reader["accPass"].ToString();
domainData.URL = reader["accDomain"].ToString();
userToAdd.DomainData.Add(domainData);
allUsers.Add(userToAdd);
}
}
For second table I have custom list that will hold the entries of all the data in second table.
The table returned is table having joins and have multiple rows for same
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了使用 Antonio Bakula 回答的字典想法之外...
如果您保留用户字典并多次调用示例中的代码,您应该考虑用户帐户是新的、修改的或删除的。
执行 SQL 查询时使用的算法如下:
我还建议不要使用 SELECT *
仅使用代码所需的表列,这可以提高代码的性能,并通过返回私有用户信息来防止潜在的安全漏洞。
Besides using the Dictionary idea as answered by Antonio Bakula...
If you persist the dictionary of users and call the code in your sample multiple times you should consider that a user account is either new, modifed, or deleted.
The algorithm to use is the following when executing your SQL query:
I'd also recommend not using SELECT *
Use only the table columns your code needs, this improves the performance of your code, and prevents a potential security breach by returning private user information.
我不知道为什么你不在你的sql中使用distinct子句来获取唯一的结果。也会更快。你看过使用哈希表吗?
i am not sure why are you not using distinct clause in your sql to fetch unique results. also that will be faster. did you look at using hashtables.
我会将用户放入字典中并检查 allready 是否存在,如下所示:
然后在 Reader while 循环中:
I would put users into Dictonary and check if allready exists, something like this :
and then in Reader while loop :
看来问题的根源在于数据库表中。
当您说重复的数据行时,您是说列表中出现重复的条目还是表中存在重复的数据?
给出 2 行重复的行。
两种选择:
首先,通过使用不同的子句来防止从 sql 中提取重复数据,例如:
选择与安东尼奥提到的第二个选项不同的地方
是检查列表是否已经有它。
除非有其他原因,否则建议选择第一个选项。
Seems like the root of your problem is in your database table.
When you said duplicate data rows, are you saying you get duplicate entries in the list or you have duplicate data in your table?
Give 2 rows that are duplicate.
Two options:
First, prevent pulling duplicate data from sql by using a distinct clause like:
select distinct from where
Second option as mentioned Antonio, is to check if the list already has it.
First option is recommended unless there are other reasons.