【发布时间】:2013-07-05 20:01:11
【问题描述】:
好的,这几天我一直在努力解决这个问题,在学习了 LINQ 之后,我认为我走在了正确的轨道上。但我有一个 SQL 大脑,有时很难将其转换为 C#。
我有两个数组,一个按字母顺序排序,另一个按 ID 排序。我需要按字母顺序排列第二个数组。 ID 是连接因素。 IE。 A.ID = P.ID。
这是我的数组和示例值;
private IGenericListItem[] _priceLevels = new IGenericListItem[0];
_priceLevels is in the form of {ID, Name}
{3, A}
{8, B}
{4, C}
{7, D}
{5, E}
{9, F}
{1, G}
编辑: 对此进行了更新以显示 _assignmentControls 包含一个子数组。我没有做出如此疯狂的借口。它实际上包含 _priceLevels 的副本...
protected ArrayList _assignmentControls = new ArrayList();
_assignmentControls is in the form of {ID, LastPrice, NewPrice, _priceLevels[]}
{1, 1.00, 2.00, _priceLevels}
{2, 1.00, 2.00, _priceLevels}
{3, 1.00, 2.00, _priceLevels}
{4, 1.00, 2.00, _priceLevels}
部分问题是我试图比较/加入 ArrayList 和 IGenericListItem。
In SQL I would do something like this;
SELECT A.*
FROM _assignmentControls A JOIN _priceLevels P
ON A.ID = P.ID
ORDER BY P.Name
这将返回一个按 _priceLevels 中的值排序的 _assignmentControls 表。
在 C# LINQ 中,我做到了这一点,但似乎无法做到;
var sortedList =
from a in _assignmentControls
join p in _priceLevels on a equals p.ID
orderby p.Name
select _assignmentControls;
我在 join 和 orderby 下得到了红色的波浪线,并且 p.Name 中的 p 是红色的。 A)它不起作用。 B) 我不确定它是否会返回 sortedList 作为按 _priceLevels.Name 排序的 _assignmentControls 的排序版本。
编辑:当我将鼠标悬停在“加入”上时,我得到“方法'IEnumerable System.Linq.Enumerable.Join(this Enumerable,IEnumerable,Func,Func...'的类型参数不能从查询中推断出来. 我现在正在研究。
感谢收看!
【问题讨论】:
-
将join中的
a改为a.ID。 -
@newStackExchangeInstance - 我改为:在 a.ID 上的 _priceLevels 中加入 p 等于 p.ID,a.ID 中的 a 现在也是红色的。这让我很困惑,因为我应该能够引用 ArrayList 的那个字段。
-
我认为你需要在最后选择一个
-
使用强类型
List<T>而不是数组列表,现在应该可以解决了。