奇怪的Linq错误 [英] Strange Linq Error

查看:74
本文介绍了奇怪的Linq错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linq将任何对象的数组转换为CSV列表:

I am using Linq to convert an array of any object to a CSV list:

String.Join(",", (From item In objectArray Select item.ToString()).ToArray())

这给了我一个奇怪的错误: 范围变量名称不能与'Object'类的成员名称匹配."

This is giving me the strange error: "Range variable name cannot match the name of a member of the 'Object' class."

我可以通过将字符串包装在VB StrConv方法中并将其设置为"Nothing"来解决这个问题:

I can get round it by wrapping the string in a VB StrConv method, with a setting of "Nothing":

String.Join(",", (From item In oArray Select StrConv(item.ToString(), VbStrConv.None)).ToArray())

但是,这似乎有点hack,我想避免这种情况.

However, this seems like a bit of a hack and I would like to avoid it.

发生此问题时,有没有人有任何想法,以及解决该问题的更好方法?

Does anyone have any ideas when this problems occurs, and any better ways to get round it?

推荐答案

将代码修改为:

String.Join(",", (From item In objectArray Select stringVal = item.ToString()).ToArray())

问题是VB为Select子句返回的变量命名.隐式地,它尝试将名称ToString赋予item.ToString(),这将与ToString方法冲突.为避免这种情况,您应该明确指定一个名称(上一行中的stringVal).

The problem is VB gives a name to the variable returned by Select clause. Implicitly, it tries to give the name ToString to item.ToString() which will clash with ToString method. To prevent so, you should explicitly specify a name (stringVal in above line).

这篇关于奇怪的Linq错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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