无法将'AnonymousType#1'类型转换为'string' [英] Cannot convert type 'AnonymousType#1' to 'string'

查看:855
本文介绍了无法将'AnonymousType#1'类型转换为'string'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在代码中的代码



 var brand_qry =( from  pd   SvarkWindow.prodlist 
join pu in SvarkWindow.produnitslist on pd.Product_id等于pu.Product_id
其中​​ pu.position> 0
选择 new {FullName = pd.Product_name + + pd.Manufacturer})。 Distinct ()。ToList();











  foreach  string  item  brand_qry)
{
if (!string.IsNullOrEmpty(Search_txt.Text))
{
< span class =code-keyword> if (item.StartsWith(typedString,StringComparison.CurrentCultureIgnoreCase))
{
autoList.Add(item);
}
}
}





i我收到错误

无法将'AnonymousType#1'类型转换为'string'



请帮我解决这个问题。

解决方案

< blockquote>嗯 - 你不能!

你正在创建一个新项集合 - 这是一个只作为Linq查询的一部分存在的类,它有一个字符串属性Fullname 。那不是字符串!



所以更改Linq:

  var  brand_qry =(来自 pd   SvarkWindow.prodlist 
join pu pdv.Product_id上的SvarkWindow.produnitslist等于pu.Product_id
其中 pu.position > 0
< span class =code-sdkkeyword> select pd.Product_name + + pd .Manufacturer).Distinct()。ToList();

这将创建一个List< string>



这就是我不喜欢的一个原因不喜欢使用var那么多 - 目前还不清楚你期望得到什么。

我会把它写成:

名单, LT;串GT; brand_qry =( from  pd  in  SvarkWindow.prodlist 
加入 pu pdv.Product_id上的SvarkWindow.produnitslist等于pu.Product_id
其中 pu.position > 0
选择 pd.Product_name + + pd.Manufacturer).Distinct()。ToList ();


brand_qry 是包含的匿名类型的集合FullName 属性,不是字符串 s的集合。



如果你想要 brand_qry 是一个字符串集合,然后用这个替换你的 select 语句:

<预lang =cs> 选择 pd.Product_name + + pd.Manufacturer





或者,如果您确实想要收集匿名类型,但仍希望迭代FullNames,然后将你的foreach声明更改为:

  foreach  brand_qry.Select中的关键字>字符串(x = >  x.FullName) )



以上语句使用选择方法选择所有 FullName s来自 brand_qry


This is my code in the down

var brand_qry = (from pd in SvarkWindow.prodlist
                                    join pu in SvarkWindow.produnitslist on pd.Product_id equals pu.Product_id
                                    where pu.position > 0
                                    select new { FullName = pd.Product_name + " " + pd.Manufacturer }).Distinct().ToList();






foreach (string item in brand_qry)
               {
                   if (!string.IsNullOrEmpty(Search_txt.Text))
                   {
                       if (item.StartsWith(typedString, StringComparison.CurrentCultureIgnoreCase))
                       {
                           autoList.Add(item);
                       }
                   }
               }



i am getting error as
Cannot convert type 'AnonymousType#1' to 'string'

please help me out of this.

解决方案

Well - you can't!
You are creating an collection of new items - which are a class that exists only as part of the Linq query, and which has a single string property "Fullname". That isn't a string!

So change the Linq:

var brand_qry = (from pd in SvarkWindow.prodlist
                 join pu in SvarkWindow.produnitslist on pd.Product_id equals pu.Product_id
                 where pu.position > 0
                 select pd.Product_name + " " + pd.Manufacturer).Distinct().ToList();

Which will create a List<string>

This is one reasons why I don't like using var that much - it isn't clear what you expect to get.
I would have written that as:

List<string> brand_qry = (from pd in SvarkWindow.prodlist
                          join pu in SvarkWindow.produnitslist on pd.Product_id equals pu.Product_id
                          where pu.position > 0
                          select pd.Product_name + " " + pd.Manufacturer).Distinct().ToList();


brand_qry is a collection of anonymous types that contain a FullName property, not a collection of strings.

If you want brand_qry to be a collection of strings, then replace your select statement by this:

select pd.Product_name + " " + pd.Manufacturer



Or, if you actually want the collection of anonymous types, but still want to iterate over the FullNames, then change your foreach statement into this:

foreach (string item in brand_qry.Select(x => x.FullName))


The above statement uses the Select method to select all FullNames from brand_qry.


这篇关于无法将'AnonymousType#1'类型转换为'string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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