Answered by:
Return multiple props using Find

Question
-
User-1129879462 posted
var project = Context.Single<Project>(p => p.ProjectCode == projectCode,
p => p.WorkItems, p => p.Notes, p => p.Resources);How can I do the same using Find?
Context.All<Project>().ToList().Find(p=>p.ProjectCode==projectCode)
returns WorkItems, Notes and Resources as null.
Thanks.
Tuesday, December 12, 2017 10:09 AM
Answers
-
User-832373396 posted
Hi Krisrajz,
krisrajz
As Find is faster than Single/First (these products TOP(x) clause which slows down) I could not use those two and would like to return only first match.Yeah, I understand you right now.
But, we still can not <g class="gr_ gr_452 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="452" data-gr-id="452">use </g><g class="gr_ gr_452 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="452" data-gr-id="452">.</g>Find(p=>p.ProjectCode==projectCode) directly; From link
For improving query performance, we could add AsNoTracking() likes the code shown below:
string city = "New York"; List<School> schools = db.Schools .AsNoTracking() .Where(s => s.City == city) .Take(1) .ToList();
Reference:
- No Tracking LINQ to Entities queries
When the context derives ObjectContext:
context.Products.MergeOption = MergeOption.NoTracking; var q = context.Products.Where(p => p.Category.CategoryName == "Beverages");
When the context derives DbContext:
var q = context.Products.AsNoTracking() .Where(p => p.Category.CategoryName == "Beverages");
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as:
- Patterns using DefaultIfEmpty for OUTER JOIN queries result in more complex queries than simple OUTER JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
Note that queries that project scalar properties are not tracked even if the NoTracking is not specified. For example:
var q = context.Products.Where(p => p.Category.CategoryName == "Beverages").Select(p => new { p.ProductName });
This particular query doesn’t explicitly specify being NoTracking, but since it’s not materializing a type that’s known to the object state manager then the materialized result is not tracked. From official document link( there are many other way to improve performance in it)
- Entity Framework and AsNoTracking
http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-and-asnotracking/
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 7:24 AM
All replies
-
User-832373396 posted
Hi <g class="gr_ gr_42 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="42" data-gr-id="42">krisrajz</g>,
Context.Single<
Context.All<
It seems that it's not Entity Framework, May I know what's this?
and I guess that could be Context.Find(p=>p.ProjectCode==projectCode) if you want to return a row record ;
With regards, Angelina Jolie
Wednesday, December 13, 2017 9:51 AM -
User-1129879462 posted
As Find is faster than Single/First (these products TOP(x) clause which slows down) I could not use those two and would like to return only first match.
Wednesday, December 13, 2017 10:58 AM -
User-832373396 posted
Hi Krisrajz,
krisrajz
As Find is faster than Single/First (these products TOP(x) clause which slows down) I could not use those two and would like to return only first match.Yeah, I understand you right now.
But, we still can not <g class="gr_ gr_452 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="452" data-gr-id="452">use </g><g class="gr_ gr_452 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="452" data-gr-id="452">.</g>Find(p=>p.ProjectCode==projectCode) directly; From link
For improving query performance, we could add AsNoTracking() likes the code shown below:
string city = "New York"; List<School> schools = db.Schools .AsNoTracking() .Where(s => s.City == city) .Take(1) .ToList();
Reference:
- No Tracking LINQ to Entities queries
When the context derives ObjectContext:
context.Products.MergeOption = MergeOption.NoTracking; var q = context.Products.Where(p => p.Category.CategoryName == "Beverages");
When the context derives DbContext:
var q = context.Products.AsNoTracking() .Where(p => p.Category.CategoryName == "Beverages");
Pros
- Improved performance over regular LINQ queries.
- Fully materialized objects.
- Simplest to write with syntax built into the programming language.
Cons
- Not suitable for CUD operations.
- Certain technical restrictions, such as:
- Patterns using DefaultIfEmpty for OUTER JOIN queries result in more complex queries than simple OUTER JOIN statements in Entity SQL.
- You still can’t use LIKE with general pattern matching.
Note that queries that project scalar properties are not tracked even if the NoTracking is not specified. For example:
var q = context.Products.Where(p => p.Category.CategoryName == "Beverages").Select(p => new { p.ProductName });
This particular query doesn’t explicitly specify being NoTracking, but since it’s not materializing a type that’s known to the object state manager then the materialized result is not tracked. From official document link( there are many other way to improve performance in it)
- Entity Framework and AsNoTracking
http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-and-asnotracking/
With regards, Angelina Jolie
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 7:24 AM -
User-1129879462 posted
Angie is great! Needless to say, everybody knows!!
Friday, December 15, 2017 12:49 PM