Return type from a Linq using a simple table
-
Thursday, July 05, 2012 3:35 PM
Very simple basic thing...but it's driving me in nuts.To be direct: What's wrong ?
public List<PAGE> ListAllPages(int PortalId, string KeyPar1, string KeyPar2, string Culture)
{
SqlServerDataContext SqlServer = new SqlServerDataContext();
var AllPages = (from p in SqlServer.PAGEs
where p.Culture.Equals(Culture)
orderby p._DateTime descending
select new { p._DateTime, p.Title, p.Tag, p.Briefing });
return AllPages.ToList();
}The return line is the error:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Newton.PAGE>' c:\users\marcojr\documents\visual studio 11\Projects\Newton\Newton\Newton.svc.cs 21 20 Newton
All Replies
-
Thursday, July 05, 2012 5:43 PM
Hi MarcoJr;
In the function signature it states that you will be returning a List<PAGE> data type but what you are returning is a collection of Anonymous type. This part of the query :
select new { p._DateTime, p.Title, p.Tag, p.Briefing });
Creates an Anonymous type that is returned in a collection.
It is not a good idea to return an Anonymous type from a function so you have a couple of options return a complete row of PAGEs which will correct the issue making the select clause as follows:
select p);
Or create a new class to hold a new type and return that to the caller of the function. So let say we create a new class called PartOfPage then you will need to change the select to this:
select new PartOfPage { DT = p._DateTime, Title = p.Title, Tag = p.Tag, Briefing = p.Briefing });
// Correct the data type for the properties
public class PartOfPage
{
public DateTime DT { get; set; }
public String Title { get; set; }
public String Tag { get; set; }
public String Briefing { get; set; }
}
and change the return type of the function to List<PartOfPage>.
Hope that helps.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Proposed As Answer by Tony xiaoModerator Monday, July 23, 2012 11:22 AM
- Marked As Answer by Tony xiaoModerator Wednesday, July 25, 2012 2:50 AM
-
Friday, July 06, 2012 7:24 AM
public List<PAGE> ListAllPages(int PortalId, string KeyPar1, string KeyPar2, string Culture)
{
SqlServerDataContext SqlServer = new SqlServerDataContext();
var AllPages = (from p in SqlServer.PAGEs
where p.Culture.Equals(Culture)
orderby p._DateTime descending
select new PAGE{_DateTime= p._DateTime, Title= p.Title, Tag=p.Tag, Briefing=p.Briefing });
return AllPages.ToList();
}Hope it can give you some help.
Best Regards,
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Proposed As Answer by Michael Samteladze Friday, July 06, 2012 11:27 AM
- Unproposed As Answer by MarcoJr Tuesday, July 10, 2012 12:12 AM
-
Tuesday, July 10, 2012 12:12 AM
Nope..I performed this way in another method..check this..
public List<CATEGORy> ListEnabledCategories(int PortalId, string KeyPar1, string KeyPar2, string Culture, int ParentId)
{
SqlServerDataContext SqlServer = new SqlServerDataContext();
var Cats = (from p in SqlServer.CATEGORies
where p.PortalId.Equals(PortalId)
where p.Culture.Equals(Culture)
where p.IsActive.Equals(true)
where p.ParentId.Equals(ParentId)
orderby p._Order
select new CATEGORy { Name = p.Name,
CustomOptions=p.CustomOptions,
Tag= p.Tag
});
return Cats.ToList();
}And the error is the same on this page :http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(EHNotSupported);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true
Because Category is a table like Page is a table too.
public List<PAGE> ListAllPages(int PortalId, string KeyPar1, string KeyPar2, string Culture)
{
SqlServerDataContext SqlServer = new SqlServerDataContext();
var AllPages = (from p in SqlServer.PAGEs
where p.Culture.Equals(Culture)
orderby p._DateTime descending
select new PAGE{_DateTime= p._DateTime, Title= p.Title, Tag=p.Tag, Briefing=p.Briefing });
return AllPages.ToList();
}Hope it can give you some help.
Best Regards,
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". -
Wednesday, July 18, 2012 12:19 PMModerator
Hi MarcoJr,
Does this issue has been resolved yet? If it doesn’t I would suggest you should get a try on Fernando’s advice or you could update the latest status of this issue here.
Best Regards,
Tony Xiao [MSFT]
MSDN Community Support | Feedback to us

