LINQ加入和顺序问题 [英] LINQ join and orderby issues

查看:170
本文介绍了LINQ加入和顺序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在撞我的头几天,在学习LINQ后,我想我在正确的轨道。但我有一个SQL大脑,有时很难翻译为C#。

OK, I've been banging my head against this for a few days, and after studying LINQ I think I am on the right track. But I have a SQL brain and that is sometimes hard to translate to C#.

我有两个数组,一个按字母顺序排序,另一个按ID排序。我需要按字母顺序排列第二个数组。 ID是连接因子。 I.E. A.ID = P.ID。

I have two arrays, one sorted alphabetically, and the other ordered by ID. I need to order the second array alphabetically. The IDs are the joining factor. I.E. A.ID = P.ID.

这是我的数组和示例值;

Here are my arrays and example values;

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的副本...

updated this to show _assignmentControls contains a sub array. I didn't make it so excuse the insanity. It actually contains a copy of _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。

Part of the problem as that I'm trying to compare/join an ArrayList and an 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表。

This Returns me an _assignmentControls table sorted by the values in _priceLevels.

得到了这么远,但看起来不能正确;

In C# LINQ I got this far, but can't seem to get it right;

        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)它不工作。我不确定它会返回sortedList作为_assignmentControls按_priceLevels.Name排序的排序版本。

I am getting red squigglies under join and orderby and the p in p.Name is red. And A) it doesn't work. B) I'm not sure it will return sortedList as a sorted version of _assignmentControls sorted by _priceLevels.Name.

编辑:当我将鼠标悬停在join类型参数的方法'IEnumerable System.Linq.Enumerable.Join(this Enumerable,IEnumerable,Func,Func ....'不能从查询中推断我现在正在研究。

When I hover over "join" I get "The type arguments for the method 'IEnumerable System.Linq.Enumerable.Join(this Enumerable,IEnumerable, Func,Func....'cannot be infered from the query. I am researching that now.

感谢您的查找!

推荐答案


我得到方法的类型参数 IEnumerable System.Linq.Enumerable.Join(这个Enumerable,IEnumerable,Func,Func .... 不能从查询。

我可以解释这里发生了什么,让您可以追踪它。

I can explain what is going on here so that you can track it down.

当您在第一次收集中从firstitem中说出

When you say

from firstitem in firstcollection
join seconditem in secondcollection on firstkey equals secondkey
select result

编译器将其转换为:

Enumerable.Join(
    firstcollection,
    secondcollection, 
    firstitem=>firstkey, 
    seconditem=>secondkey, 
    (firstitem, seconditem)=>result)

Enumerable.Join 是一个具有四个类型参数的通用方法:第一个集合的元素类型,第二个集合的元素类型,键类型和结果类型。

Enumerable.Join is a generic method that has four type parameters: the element type of the first collection, the element type of the second collection, the key type, and the result type.

如果你得到那个错误,那么这四个东西中的一个不能推断给你的信息,你提供给编译器。例如,也许:

If you're getting that error then one of those four things cannot be deduced given the information you've provided to the compiler. For example, maybe:


  • 第一个集合的类型实际上不是一个序列。

  • 第二个集合的类型实际上不是序列。

  • 无法推导出结果的类型

  • 两个键的类型不一致,没有唯一的最佳类型。

  • The type of the first collection is not actually a sequence.
  • The type of the second collection is not actually a sequence.
  • The type of the result cannot be deduced
  • The two keys are of inconsistent types and there is no unique best type.

最后一个点是最可能的一个点。假设例如第一个键是 int ,第二个键是 short 。由于每个 short 可以转换为 int int win,第二个键会自动转换为 int 。现在假设第一个键类型是 Giraffe ,第二个键类型是 Tiger 。两者都不比另一个更好。 C#不说哦,他们都是 Animal 的,所以让我们选择。相反,它说,你没有提供足够的信息,以确定你的意思;你应该把其中一个转到 Animal ,然后就会变得清楚。

That last point is the most likely one. Suppose for example the first key is int and the second key is short. Since every short can be converted to int, int would win, and the second key would be automatically converted to int. Now suppose that the first key type is Giraffe and the second key type is Tiger. Neither is better than the other. C# does not say "oh, they're both kinds of Animal, so let's pick that." Rather, it says that you haven't provided enough information to determine which one you meant; you should cast one of them to Animal and then it becomes clear.

有意义吗?

我有一个半小时的视频,我在2006年解释这个功能 - 这是回来的时候,我正在向编译器添加的功能,所以如果你想要一个更多的,深入解释,请查看。

There's a half-hour video of me explaining this feature back in 2006 -- this was back when I was adding the feature in question to the compiler -- so if you want a more in-depth explanation, check it out.

http://ericlippert.com/2006/11/17/a-face-made-for-email-part-three/

更新:我再仔细阅读您的问题:

UPDATE: I just read your question again more carefully:


部分问题,我试图比较/加入 ArrayList IGenericListItem

有问题。序列的类型不能由 ArrayList 确定。你不应该使用 ArrayList 。事实上,你不应该在2005年以后编写的任何代码中使用它。使用列表< T> 为一些合适的T。

There's the problem. The type of the sequence cannot be determined from an ArrayList. You should not use ArrayList anymore. In fact, you should not use it in any code written after 2005. Use List<T> for some suitable T.

这篇关于LINQ加入和顺序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆