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
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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.
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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;
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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...
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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;
re: Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1
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...


Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1 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 error var v = (( from c in dataContext.Locales where (c.CountryCode = = CountryCode) select new {c.LanguageCulture}).Tostring()); return v; Then 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 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
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 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 over XML
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 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 is also
LINQ LINQ IN .NET Hi, here is a good article for you www.eggheadcafe.com / tutorials / aspnet / 042c0b06-95f2-4944-9b52-46be6eeb3e7d / linq-executed-in-parallel-plinq.aspx as well here is some good link from where you can learn more about linq http: / / msdn.microsoft.com / en-us / library / bb907622.aspx http weblogs.asp.net / scottgu / archive / 2006 / 05 / 14 / Using-LINQ-with-ASP.NET-_2800_Part-1_2900_.aspx http: / / weblogs.asp.net / jalpeshpvadgama / archive / 2008 / 07 / 31 / what-is-linq-linq-developer-resources.aspx Hope this will help you Hi LINQ is a uniform programming model for any kind of data access in .Net framework LINQ is all about query data. Read more here http: / / geekswithblogs http: / / en.csharp-online.net / Introducing_LINQ%E2%80%94What_Is_LINQ Create New Account keywords: LINQ description: LINQ IN .NET
why to use linq why to use linq There are several reasons to why you would choose to use LINQ. First of let me clarify a common misconception when people first learn about LINQ. LINQ itself has nothing to do with SQL or databases. There LINQ is an integrated query language for any collection; in fact anything that implements IENUMBERABLE. There is an added technology called LINQ to SQL which handles the connections to the database. I are two main reasons why you would choose to use LINQ to SQL. First There are some very good performance improvements to Rico Mariani’s blog. Second is the code generation. LINQ to SQL will generate object classes for you and provide a disclaimer I am by no means an expert on LINQ but I have been using it for quite some time
LINQ With Strings Some interesting approaches to string querying and conversion via LINQ LINQ seems to have found its way into the vocabulary of the average .NET Developer. I find myself using LINQ more and more often, usually as a quick way to manipulate objects, sort them, filter and so on. LINQ can be used to query and transform strings and collections be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular strings that you can then query or modify by using LINQ. You can use the IsMatch method in the where clause of a LINQ query. And you can use LINQ to query or modify the MatchCollection results returned by a some interesting tidbits of sample code to manipulate strings with LINQ. All of them are assembled in a single project that
LINQ in asp.net I want to know the very basics of linq in asp.net? How to use this in asp.net? Regards kiruba.e Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 a popular set of samples that demonstrate various aspects of LINQ, samples for both Visual C# and Visual Basic. These cover com / en-us / vstudio / aa336746 What is the purpose of LINQ Providers in LINQ? LINQ Providers are a set of classes that takes a LINQ query and dynamically generates a method that executes an equivalent query against a specific data source. What are the four LINQ Providers that .NET Framework ships? 1. LINQ to Objects - Executes a LINQ query against a collection of