【发布时间】:2013-06-17 18:48:50
【问题描述】:
假设我有 两个 List<string>。这些是从读取文本文件的结果中填充的
列表所有者包含:
cross
jhill
bbroms
列出受让人包含:
Chris Cross
Jack Hill
Bryan Broms
在从 SQL 源读取期间(SQL 语句包含连接)...我会执行
if(sqlReader["projects.owner"] == "something in owner list" || sqlReader["assign.assignee"] == "something in assignee list")
{
// add this projects information to the primary results LIST
list_by_owner.Add(sqlReader["projects.owner"],sqlReader["projects.project_date_created"],sqlReader["projects.project_name"],sqlReader["projects.project_status"]);
// if the assignee is not null, add also to the secondary results LIST
// logic to determine if assign.assignee is null goes here
list_by_assignee.Add(sqlReader["assign.assignee"],sqlReader["projects.owner"],sqlReader["projects.project_date_created"],sqlReader["projects.project_name"],sqlReader["projects.project_status"]);
}
我不想最终使用嵌套的 foreach。
FOR 循环可能就足够了。有人向我提到了 ZIP,但不确定在我的情况下这是否是更可取的途径。
【问题讨论】:
-
iterate through both lists是指仅比较alpha中的第一项与beta中的第一项,然后将第二项与第二项进行比较,以此类推向前?如果是这样,只需使用常规的for循环。 -
你考虑过只使用
LINQ的.Contains吗? (比如 'alpha.Contains(result["chestA.item"]))` 还是我误解了你的支票?似乎根本不需要任何嵌套。也许你会连续有两个循环,但这只是O(2*n),它仍然是O(n)。 -
这感觉需要澄清一下。您有两个大小相同的列表,并且您想对它们做什么 what,究竟是什么?你从 SQL 源代码中读到了什么?你想用你的清单做什么?您是说您正在获得一对项目(A 和 B)并且您正在尝试查看 (A) 是否在列表 alpha 中或 (B) 是否在列表 beta 中?
-
感谢您的帮助,我添加了一些说明(希望有帮助)