Johan Alkstål
Johan Alkstål

Reputation: 5347

Changing an extention method from linq-to-sql to entity framework

I'm changing my project from working with Linq-to-SQL to working with Entity Framework.

I have some extention methods that extend the classes created by LINQ and I'm wondering how to change them to work with the entities instead

Here's an example.

public static int GetPublishedArticlesCount(this Table<Article> source)
    {
        return GetPublishedArticles(source.Context as DataContext, null).Count();
    }

This method gets the number of published articles. Instead of using this Table<Article>, what should I use instead?

Upvotes: 0

Views: 172

Answers (3)

Sunny
Sunny

Reputation: 6336

As the previous poster metioned you could use ObjectQuery to get the result.

To make the method more generic as an extension method to ObjectQuery & get count of any type perhaps this might help:

public static int GetCount<T>(this ObjectQuery<T> query){
    return query.CreateQuery<T>(typeof(T).Name).Count();
}

NOTE: I am writing this code from top of my head, might has some sytax errors...

HTH.

Upvotes: 0

Simon P Stevens
Simon P Stevens

Reputation: 27499

You probably need to use the ObjectQuery<T> class.

public static int GetPublishedArticlesCount(this ObjectQuery<Article> source)

Upvotes: 2

EgorBo
EgorBo

Reputation: 6142

Analog of Table<> (linq 2 sql) in EntityFramework is ObjectQuery

Upvotes: 1

Related Questions