Linq,VB-匿名类型不能转换为匿名类型 [英] Linq, VB - Anonymous type cannot be converted to anonymous type

查看:36
本文介绍了Linq,VB-匿名类型不能转换为匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq noobie,也许有人可以指出我正确的方向.怎么了这些匿名类型似乎具有相同的签名.

          '*** Get all of the new list items'
          Dim dsNewFiles = From l1 In list1 _
                           Where Not (From l2 In list2 _
                                      Select l2.id, l2.timestamp).Contains(New With {l1.id, l1.timestamp})

我希望上面的代码中有一些突出显示的方法,但是出现编译错误:

Value of type '<anonymous type> (line n)' cannot be converted to '<anonymous type> (line n)'.

在".Contains(新增了{l1.id,l1.timestamp} )"上

我假设它认为匿名类型在某些方面有所不同,但是两个列表中的id和timestamp列均相同.它们也以相同的顺序.两者之间还有什么不同?

我尝试了来自用户Meta-Knight的建议代码(带有{Key l1.id,l1.timestamp}的新功能),它修复了编译错误.但是,当我使用List1和List2运行代码时,如下所示:

List1                         List2
id     timestamp              id     timestamp
--     ----------             --     ----------
01     2009-07-10 00:00:00    01     2009-07-10 00:00:00

结果是:

dsNewFiles
id     timestamp
--     ----------
01     2009-07-10 00:00:00

它应该是一个空列表.

解决方案

只需将代码的最后一部分更改为:

New With {Key l1.id, Key l1.timestamp}

我测试了代码,并且可以正常工作.

我不知道为什么这对您不起作用,为了确定起见,我将发布整个代码.

Dim dsNewFiles = From l1 In list1 _
                           Where Not (From l2 In list2 _
                                      Select l2.ID, l2.TimeStamp).Contains(New With {Key l1.ID, Key l1.TimeStamp})

另一种选择是简单地执行以下操作:

Dim dsNewFiles = list1.Except(list2)

为此,您的类必须重写Equals和GetHashCode,并实现IEquatable(Of T)接口.在MSDN上(底部)有一个很好的示例 . /p>

如果ID和Timespan在类中不表示相等性,则可以使用自定义IEqualityComparer(Of T)作为第二个参数.

I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures.

          '*** Get all of the new list items'
          Dim dsNewFiles = From l1 In list1 _
                           Where Not (From l2 In list2 _
                                      Select l2.id, l2.timestamp).Contains(New With {l1.id, l1.timestamp})

I wish there were some way to highlight in the above code, but I get the compile error:

Value of type '<anonymous type> (line n)' cannot be converted to '<anonymous type> (line n)'.

on the ".Contains(New With{l1.id, l1.timestamp})"

I assume it thinks the anonymous types are different in some way, but the id and timestamp columns are the same in either list. They are also in the same order. What else can be different between the two?

[Edit 7/10/2009 16:28 EST]

I tried the suggested code from user Meta-Knight (New With {Key l1.id, l1.timestamp}) and it fixed the compile error. However, when I ran the code with List1 and List2 as follows:

List1                         List2
id     timestamp              id     timestamp
--     ----------             --     ----------
01     2009-07-10 00:00:00    01     2009-07-10 00:00:00

The result was:

dsNewFiles
id     timestamp
--     ----------
01     2009-07-10 00:00:00

It should have been an empty list.

解决方案

Just change the last part of your code to:

New With {Key l1.id, Key l1.timestamp}

I tested the code and it works.

Edit:

I don't know why this doesn't work for you, I'll post the whole code just to be sure.

Dim dsNewFiles = From l1 In list1 _
                           Where Not (From l2 In list2 _
                                      Select l2.ID, l2.TimeStamp).Contains(New With {Key l1.ID, Key l1.TimeStamp})

Another option is to simply do the following:

Dim dsNewFiles = list1.Except(list2)

For this to work, your class must override Equals and GetHashCode, and implement the IEquatable(Of T) interface. There's a very good example on MSDN (at the bottom).

If ID and Timespan don't represent equality in your class, you can use a custom IEqualityComparer(Of T) as a second argument.

这篇关于Linq,VB-匿名类型不能转换为匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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