Entity framework with Linq to Entities performance?

New connections are non-expensive thanks to connection caching. Basically, it grabs an already open connection (I htink they are kept open for 2 minutes for reuse) Still, caching may be better. I do really not like the "firstordefault".

Thinks of whether you can acutally pull in more in ONE statement, then work from that For the rest, I can not say anything - too much depends on what you actually do there logically. What IS TicDatabaseEntities? CAN it be cached?

How long? Same with (3) - we do not know because we do not know what else is in there If this is something like getting just some lookup strings for later use, I would say Build a key out of classI, class II, class III load all classifications in (I assume there are only a couple of hundred) Put them into a static / cached dictionary, assuming they normally do not change (and I htink I have that idea here - is this a financial tickstream database? ) Without business knowledge this can not be answered 4: yes, that is as documented (RTFM hint).

First gives first or an exception, FirstOrDefault defaults to default (empty struct initialized with 0, null for classes).

New connections are non-expensive thanks to connection caching. Basically, it grabs an already open connection (I htink they are kept open for 2 minutes for reuse). Still, caching may be better.

I do really not like the "firstordefault". Thinks of whether you can acutally pull in more in ONE statement, then work from that. For the rest, I can not say anything - too much depends on what you actually do there logically.

What IS TicDatabaseEntities? CAN it be cached? How long?

Same with (3) - we do not know because we do not know what else is in there. If this is something like getting just some lookup strings for later use, I would say.... Build a key out of classI, class II, class III load all classifications in (I assume there are only a couple of hundred) Put them into a static / cached dictionary, assuming they normally do not change (and I htink I have that idea here - is this a financial tickstream database? ) Without business knowledge this can not be answered.4: yes, that is as documented (RTFM hint).

First gives first or an exception, FirstOrDefault defaults to default (empty struct initialized with 0, null for classes).

TicDatabaseEntities is an Entity Data Model (EF) context. Could you explain in more detail why don't you like firstordefault()? – mare May 14 '10 at 18:21.

Thanks Dan and TomTom, I've came up with this. Could you please comment this if you see anything out or the order? Public static IEnumerable TicArticles { get { ObjectCache cache = MemoryCache.

Default; if (cache"TicArticles" == null) { CacheItemPolicy policy = new CacheItemPolicy(); using(TicDatabaseEntities db = new TicDatabaseEntities()) { IEnumerable articles = (from a in db. Articles select a).ToList(); cache. Set("TicArticles", articles, policy); } } return (IEnumerable)MemoryCache.

Default"TicArticles"; } } private static bool TicArticleExists(string supplierIdent) { if (TicArticles. Count(p => p. SupplierArticleID.

Equals(supplierIdent)) > 0) return true; return false; } If this is ok, I'm going to make all my method follow this pattern.

No. Connections are cached. Should I cache the results somewhere No.

Do not cache entire tables. Should I make TicDatabaseEntities variable static and initialize it at class level? No.Do not retain a DataContext instance longer than a UnitOfWork.

Should my class be static if it contains only static methods?Sure... doing so will prevent anyone from creating useless instances of the class. Also I've noticed that if I return result.First() instead of FirstOrDefault() and the query does not find a match, it will issue an exception That is the behavior of First.As such - I typically restrict use of First to IGroupings or to collections previously checked with .Any(). I'd rewrite your existing method as: using (TicDatabaseEntities db = new TicDatabaseEntities()) { bool result = db.

Articles . Any(a => a. SupplierArticleID.

Equals(supplierIdent)); return result; } If you are calling the method in a loop, I'd rewrite to: private static Dictionary TicArticleExists (List supplierIdents) { using (TicDatabaseEntities db = new TicDatabaseEntities()) { HashSet queryResult = new HashSet(db. Articles . Where(a => supplierIdents.

Contains(a. SupplierArticleID)) . Select(a => a.

SupplierArticleID)); Dictionary result = supplierIdents . ToDictionary(s => s, s => queryResult. Contains(s)); return result; } }.

Very nice, thank you! – mare May 15 '10 at 11:17 After accepting your answer, I found out that the first piece of code you provided (using Any()) takes much longer to execute vs my previous code based on Count(). The second method has some issue too - specifically it timeouts when making the HashSet.

– mare May 15 '10 at 14:05 Use db. Log = Console. Out; to dump out the query used.

Find the difference in the query. – David B May 16 '10 at 0:01.

I'm trying to find the article where I read this, but I think it's better to do (if you're just looking for a count): from a in db. Articles where a. SupplierArticleID.

Equals(supplierIdent) select 1 Also, use Any instead of Count > 0. Will update when I can cite a source.

To execute a LINQ to Entities query against the Entity Framework, the LINQ query must be converted to a command tree representation that can be executed against the Entity Framework. LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact. LastName, and so on).

LINQ operators are not defined by a class, but rather are methods on a class. In LINQ, expressions can contain anything allowed by types within the System.Linq. Expressions namespace and, by extension, anything that can be represented in a lambda function.

This is a superset of the expressions that are allowed by the Entity Framework, which are by definition restricted to operations allowed on the database, and supported by ObjectQuery. In the Entity Framework, both operators and expressions are represented by a single type hierarchy, which are then placed in a command tree. The command tree is used by the Entity Framework to execute the query.

If the LINQ query cannot be expressed as a command tree, an exception will be thrown when the query is being converted. The conversion of LINQ to Entities queries involves two sub-conversions: the conversion of the standard query operators, and the conversion of the expressions. There are a number of LINQ standard query operators that do not have a valid translation in LINQ to Entities.

Attempts to use these operators will result in an exception at query translation time. For a list of supported LINQ to Entities operators, see Supported and Unsupported LINQ Methods (LINQ to Entities). For more information about using the standard query operators in LINQ to Entities, see Standard Query Operators in LINQ to Entities Queries.

In general, expressions in LINQ to Entities are evaluated on the server, so the behavior of the expression should not be expected to follow CLR semantics. For more information, see Expressions in LINQ to Entities Queries. For information about how CLR method calls are mapped to canonical functions in the data source, see CLR Method to Canonical Function Mapping.

For information about how to call canonical, database, and custom functions from within LINQ to Entities queries, see Calling Functions in LINQ to Entities Queries.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions