SQL query:
SELECT ArticleCategories.Title AS Category, Articles.Title, Articles.[Content], Articles.Date
FROM ArticleCategories INNER JOIN
Articles ON ArticleCategories.CategoryID = Articles.CategoryID
Object repository:
public class ArticleDisplay
{
public int CategoryID;
public string CategoryTitle;
public int ArticleID;
public string ArticleTitle;
//public string ArticleDate;
public string ArticleContent;
}
public class ArticleRepository
{
private DB db = new DB();
//
// Query Methods
public IQueryable<ArticleDisplay> FindAllArticles()
{
var result = from category in db.ArticleCategories
join article in db.Articles on category.CategoryID equals article.CategoryID
开发者_StackOverflow select new
{
CategoryID = category.CategoryID,
CategoryTitle = category.Title,
ArticleID = article.ArticleID,
ArticleTitle = article.Title,
//ArticleDate = article.Date,
ArticleContent = article.Content
};
return result;
}
....
}
And finally, I get this error:
Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\ilija\My Documents\Visual Studio 2008\Projects\CMS\CMS\Models\ArticleRepository.cs 29 20 CMS
Any idea what did I do wrong?
Thanks,
IleYour method has a return type of IQueryable<Article>, but your LINQ query is not returning articles. Instead it's trying to return an anonymous type made up of properties from both category and article. You could create a class to hold this combination of properties and change the method signature to use that class.
As Adam said, if you're not wanting to return the full article class then you'll need to return your own class.
public IQueryable<ArticleDisplay> FindAllArticles()
{
var result = from category in db.ArticleCategories
join article in db.Articles on category.CategoryID equals article.CategoryID
select new ArticleDisplay() //specify your new class
{
CategoryID = category.CategoryID,
CategoryTitle = category.Title,
....
};
return result;
}
Your updated code still returns an anonymous class....
精彩评论