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

Asked By anamika shah
03-Aug-10 11:41 PM
Earn up to 0 extra points for answering this tough question.

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

{

 

Rent2BuyEntities dataContext = new Rent2BuyEntities(System.Configuration.ConfigurationManager.ConnectionStrings["Rent2BuyEntities"].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

Christiano Coutinho replied to anamika shah
07-Aug-10 01:06 PM
Hi there!

Your query returns an IEnumerable class containing an anonymous type whose propertires are defined on the select clause. So, you have 2 options: 

1 - Return a List from your query 
2 - use the extension method First, if you need only the first item or if your query always returns a single result


1 )

IList<string> v = ((from c in dataContext.Locales

where (c.CountryCode == CountryCode)

select c.LanguageCulture).Tostring()).ToList();

return v;


2) string resp = (from c in dataContext.Locales where c.ContryCode == ContryCode) select c.LanguageCulture.ToString()).First();


Kind regards
Create New Account
I am tryihg to joi nmore than one field in linq listModel.AddRange(( from veh in dataContext.vwVehicle join model in Year) > = 0) && ( string .IsNullOrEmpty(make) | | make.IndexOf(veh.Make) > = 0) && countryCode = = veh.countrycode orderby veh.Model select new IdNamePair () { Id = veh.Model, Name = model.ModelLocalized }).Distinct().ToList()); I am getting error name model is ot in scope Year) > = 0) && ( string .IsNullOrEmpty(make) | | make.IndexOf(veh.Make) > = 0) && countryCode = = veh.countrycode orderby veh.Model select new IdNamePair() { Id = veh.Model, Name = model.ModelLocalized }).Distinct().ToList()); Create New Account
public static List < IdNamePair > GetModels( string year, string make, string countryCode, string languageCulture) { if (listModel = = null ) { listModel = new List < IdNamePair > (); using Year) > = 0) && ( string .IsNullOrEmpty(make) | | make.IndexOf(veh.Make) > = 0) && countryCode = = veh.countrycode orderby veh.Model select new IdNamePair () { Id = veh.Model, Name = model.ModelLocalized }).Distinct().ToList()); } return listModel; } I am using list to return model id and model name.List use linq query. I am joining table model and vwvehicle on veh or group clause error. Can anyone help me with the Linq. Even I am getting error Operator && can't be applied getting The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties
completely eliminate the necessity for ORM’s such as NHibernate, LINQ to SQL, or Entity Framework, since you can store your you can search and query it back out with familiar LINQ semantics, again, usually in one or two lines of code new XmlDocument(); doc.LoadXml(s); stat.Country = doc.DocumentElement.SelectNodes( "CountryCode" )[0].InnerText; stat.State = doc.DocumentElement.SelectNodes( "RegionName" )[0].InnerText XElement doc = XElement.Parse(s); stat.Country = (string) doc.Element("CountryCode"); stat.State = (string)doc.Element("RegionName"); stat.City = (string)doc Stat> (); this .GridView1.DataSource = coll.AsQueryable().OrderByDescending(s = > s.LogDateTime ).ToList(); GridView1.DataBind(); } Lastly, I have a CreateIndex page that calls
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