如何转换类属性 [英] how to cast class property
问题描述
你好,
我的代码如下。
Hello,
I have code like below.
var ds = from DataRowView rowView in view
select new CM.Models.ClassName
{
Note = rowView["Type"].ToString(),
};
method.Welcome((ClassName)ds).Process();
当我像这样投它会给我以下错误。
无法转换'WhereSelectEnumerableIterator`2类型的对象[System.Data.DataRowView,ClassName ClassName
任何人都可以帮我吗?
问候,>
Viprat
When i cast like this it will give me below error.
Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Data.DataRowView,ClassName ClassName
Can any one help me on this?
Regards,
Viprat
推荐答案
Linq查询返回你的类的IEnumerable - 你可以把它转换为单个值,而不是你可以使用它一个整数数组可以进行简单的算术运算:
The Linq query returns an IEnumerable of your class - and you can;t cast that to a single value any more than you can use an array of integers tin a simple arithmetic operation:
int[] ar = GetInts();
int x = ar + 6;
相反,要么使用FirstOrDefult返回单个项目,要么检查计数并获取集合的第一个成员以传递给您的方法。这两种方式都不需要强制转换,因为Linq查询专门返回一个命名类型的集合。
Instead, either return a single item using FirstOrDefult, or check the count and fetch the first member of the collection to pass to your method. Neither way will need casting as the Linq query specifically returns a collection of a named type.
这篇关于如何转换类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!