Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code in the down

SQL
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();





C#
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.
Posted
Updated 21-Jul-15 1:00am
v2

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:
C#
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:
C#
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();
 
Share this answer
 
Comments
Thomas Daniels 21-Jul-15 6:51am    
+5
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:
C#
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:
C#
foreach (string item in brand_qry.Select(x => x.FullName))

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900