score:0
I use LINQ to map domain objects to view models. (I use the Entity Framework, but the technique works in any ORM with good LINQ support.) First write a POCO view model, then project onto it:
var pm = from c in Context.Cities
let score = c.Scores.Where(s => s.MonthCode == selectedMonthCode).FirstOrDefault()
select new CityScoresPresentation
{
City = c.Name,
Score = score.Score
};
This is LINQ to Entities. LINQ to SQL works similarly. YMMV with other ORMs LINQ implementations.
I strongly recommend using a view model instead of binding views to ORM-aware types.
score:2
We actually practice a form of Command Query Separation (Greg Young version -- not the Meyer version) so we would at the very least use the NHibernate ICriteria to select the details we want and then use the AliasToBeanTransformer to inject them directly into a DTO.
For our most complex entities we actually have a separated table with all the details a screen needs to display collapsed into a single row (aka OLAP). We can then run queries directly against this table which by-passes the cost of loading a complex entity which contains much more information then our screen needs.
Source: stackoverflow.com
Related Articles
- .NET - ORMs and all possible combinations - ViewModel?
- Is it possible to express this code in LINQ?
- Why is my code doing lazy loading even after I turned it off at every possible point?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Create all possible combinations of items in a list using Linq and C#
- .NET String parsing performance improvement - Possible Code Smell
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- finding possible combinations linq
- creating Linq to sqlite dbml from DbLinq source code
- How to debug this or possible causes? System.NullReferenceException was unhandled by user code on Moq.Verify
- How to get all possible combinations from an array?
- Is it possible to rewrite the below code in Linq/Lambda approach
- Make access possible to dynamic table LINQ EF6 Code First
- Is it possible to have one LINQ in one line of code for
- Code First EF: While searching database table is it possible to retrieve list of items it has in DataModel?
- How to handle nulls in this LINQ Code using a possible null List?
- source code for LINQ 101 samples
- Code First Entity Framework, select ViewModel - constructor with parameter
- VS Code Coverage won't recognize only possible Expression Lambda Path
- MVC5 Linq to ViewModel to Razor View code improvement?
- How do I find all possible combinations of the form ±1 ± 2 ± 3 ± ... ± n = k with LINQ?
- List or Array of String Contain specific word in Html Source Code
- linq query to get possible combinations
- c# Linq or code to extract groups from a single list of source data
- Is possible to rewrite the code so as to reduce the number of selects?
- All possible combinations from given sets but without repetition of sets’ internal elements
- How to insert a record in lambda expression and possible way to shorten the length of code
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Like operator in Expression Tree
- Linq sum anonymous collections to find total in View
- QueryExtender Linq Orderby using TimeSpan.Parse
- C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?
- Hashcode to check uniqueness in a string array
- How to call a function inside a where clause of IQueryable
- linq lambda multiple group joins
- Why EF generating a sub-query for a simle query?
- LINQ Searching ordered list and appending occurence number
- LINQ: Get the path in a hierarchical structure collection item
- Linq Dynamic Error with Dynamic Where clause
- OOP approach for the Triangle Check scenario
- Using two queries in the same controller for same view using - User.Identity.Name
- Poor performance in tree pruning
- Linq opening and closing parentheses does not match
- How to get pair value from CultureTypes object in C#?
- (JSON.NET) RuntimeBinderException accessing dynamic JObject (works on one PC, fails on another)
- Assigning ID to Objects transmitted in XML document
- Function evaluation disabled, Error in VS 2015 on Query LinQ Execution?
- LINQ Remove duplicates and result item to have sum of quantities