views:

43

answers:

2

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
                           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,
Ile

+2  A: 

Your 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.

Adam
Could you please tell me how exactly should I make this new class? I'm pretty new to C# so I'm having big troubles with this one...This is what I tried:public class ArticleDisplay { public string CategoryID; public string CategoryTitle; public string ArticleID; public string ArticleTitle; public string ArticleDate; public string ArticleContent; }
ile
You need to match the data types (are they really all string?) but, yes, a simple class like that would do.
Adam
I edited my question... still doesn't work
ile
Finally solved this! Thanks!
ile
+1  A: 

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....

mezoid
Yap, I forgot to add ArticleDisplay() after "select new"... thanks!
ile