LINQ - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1

Asked By anamika shah
03-Aug-10 11:49 PM

I have Method which use Linq as follow
public static string GetLocale(string CountryCode)

{

 

Entities dataContext = new Entities(System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ConnectionString);

 

var v = ((from c in dataContext.Locales

where (c.CountryCode == CountryCode)

select new {c.LanguageCulture}));

return v;

 

 

}

I have Locales table used in Linq as follow.

Id(int)    CountryCode(nvarchar)       LanguageCulture(nvarchar)
1    US                    en-US
2    FR                    fr-FR

CountryCode and LanguageCulture both are nvarchar.Then when I run this method I error like
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1 to string.
If I write Tostring() to remove that error

var v = ((from c in dataContext.Locales

where (c.CountryCode == CountryCode)

select new {c.LanguageCulture}).Tostring());

return v;

Then I get v=  "System.Data.Objects.ObjectQuery`1[<>f__AnonymousTypea`1[System.String]]" string

I want v=fr-FR or v=en-US based on CountryCode passed.
If CountryCode passed is US I should get v=en-US an dif CountryCode is FR then I should get
v=fr-FR

 

 

 

 

 

 

 

 

 

  samjayander thiagarajan replied to anamika shah
04-Aug-10 12:46 AM
Hi,

In your code the LINQ statement will fetch the results only when it is accessed.

Also, it can return a list of strings, so you have to single out the item.

You can do it like this:

var v = ((from c in dataContext.Locales
where (c.CountryCode == CountryCode)
select new {c.LanguageCulture}).FirstorDefault();
 
if (v != null)
 return v.ToString();


Regards,
Sam.
  Mash B replied to anamika shah
04-Aug-10 02:29 AM
Below are ways to achive it
 
Method 1:
 
var v = ((from c in dataContext.Locales
 
where (c.CountryCode == CountryCode)
 
select new { Culture = c.LanguageCulture}));
 
return v.Culture;
 
Method 2:
 
var v = from c in dataContext.Locales
 
where (c.CountryCode == CountryCode)
 
select c;
 
return v.LanguageCulture;
  anamika shah replied to samjayander thiagarajan
04-Aug-10 04:01 AM
I am gettting error lnear now  FirstorDefault

Error 21 'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'FirstorDefault' and no extension method 'FirstorDefault' accepting a first argument of type 'System.Linq.IQueryable<AnonymousType#1>' could be found (are you missing a using directive or an assembly reference?) 

Please help me soon.Thanks in advance.It is like emergency
  anamika shah replied to Mash B
04-Aug-10 04:08 AM
Mash I tried method 1) and method 2) both are throwing now this error..

Error 21 'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Culture' and no extension method 'Culture' accepting a first argument of type 'System.Linq.IQueryable<AnonymousType#1>' could be found (are you missing a using directive or an assembly reference?)


Please can you help me fast...
  Mash B replied to anamika shah
04-Aug-10 04:45 AM
Try this one

var v = from c in dataContext.Locales
 
where (c.CountryCode == CountryCode)
 
select c;
 
 string result = "";
 foreach(var item in v)
 {
    result = item["LanguageCulture"].ToString();
 }

 return result;
  anamika shah replied to Mash B
04-Aug-10 05:07 AM
I am getting LanguageCultue="fr-FR" when I pass CountryCode as fr.

var v = ((from c in dataContext.Locales

where (c.CountryCode == CountryCode)

select new { c.LanguageCulture }).FirstOrDefault());

return v.ToString();

 

when I use above Linq.
But inmy database I have data as follow.

CountryCode  LanguageCulture
FR          fr-FR

But query returns LanguageCulture="fr-FR"
I don't want LanguageCulture=''fr-FR" I just want query to return fr-FR.
Because in my all table I have comibnation as I mentioned above.When I try to use this method it returns LanguageCulture="fr-FR".Can you please help me with this..It is urgent...


Create New Account
help
LanguageCulture = lines[1].Replace( " \ "" , "" ); I am using method as below. public static string GetLocale( string CountryCode) { Entities dataContext = new Entities (System.Configuration. ConfigurationManager .ConnectionStrings[ "Entities" ].ConnectionString); var v = (( from c in dataContext.Locales where (c.CountryCode = = CountryCode) select new { sLocale = c.LanguageCulture }).FirstOrDefault()); return v.ToString(); This method gives me LanguageCulture = "en-US" I need this method to you dont make things complicated, below is the easiest way you get your data in LINQ query itself. var v = (from c in dataContext.Locales where (c.CountryCode = = CountryCode) select new { sLocale = c.LanguageCulture }).FirstOrDefault(); string yourResult = v.sLocale.ToString(); You have put that code before returning value. And change
LINQ Hi All Why we use LINQ and what is diffrence between LINQ and SQL? LINQ which I think is one of the most exciting features in Orcas. LINQ makes the concept of querying a first-class programming concept in .NET. The data to be queried can take the form of XML ( LINQ to XML), databases ( LINQ -enabled ADO.NET: LINQ to SQL, LINQ to Dataset and LINQ to Entities) and objects ( LINQ to Objects). LINQ
linq? can someone send me a good link where i can start learning linq? LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set for queries and provides class libraries to take advantage of these capabilities. Featured Information Essential LINQ (Microsoft .NET Development Series) LINQ is one of Microsoft’s most exciting, powerful new development technologies. Essential LINQ is the first LINQ book written by leading members of Microsoft’s LINQ and C# teams. LINQ to XML LINQ to XML was developed with Language-Integrated Query