score:7
Don't select everything in memory first. Do something like this:
public IQueryable<ItemDto> Get()
{
using (EfContext context = new EfContext())
{
var query = from item in context.Items
select Mapper.Map<Item, ItemDto>(item)
return query.OrderByDescending(x => x.PubDate).Take(20));
}
}
BTW The following code is something you want to do once, for example in a static constructor or in the WebApiConfig.cs
file.
Mapper.CreateMap<Item, ItemDto>()
.ForMember(itemDto => itemDto.Category, mce => mce.MapFrom(item => item.Category.Name));
score:3
If the only querying takes place in the code we see (i.e. ordering and Take ) your code is fine. It will only map the result (max 20). However, since you are returning IQueryable I assume you'd like to further query the result. May be OData style parameters?
With max 20 items you're probably better off not writing any code. The rest of the queries will be performed as object queries. However, if you decide to remove that constraint (max 20) or put that after further queries are made then this way will be inefficient.
Basically, you need to move the mapping at the very end of the query chain if you'd like all your queries run in the EF database.
What you can do is actually return the actual entity objects
public IQueryable<ItemDto> Get()
{
using (EfContext context = new EfContext())
{
return context.items
.OrderByDescending(x => x.PubDate)
.Take(20));
}
}
And tell MVC how to serialize this in a MediaTypeFormatter. Here you could use the AutoMapper.
score:3
http://dotnetplusplus.wordpress.com/2013/08/30/odata-web-apis-with-automapper-3/
Use return _itemRepository .GetItemsQuery() .Project().To();
Source: stackoverflow.com
Related Query
- ASP.NET Web API return queryable DTOs?
- How to cast my Linq query to the expected Web API return type
- Web API 2. Return Description instead of Int Join/Include?
- How to use web api to return a collection (array) of arrays of unlabeled values
- LINQ Recursive query in web api code
- Efficient way to loop through data to return Object in Web api c#
- Trying to insert multiple rows ASP Web API with Postman in JSON
- web api return complex object not getting serialized
- Using Cross join in Asp .net Web API getting error
- How do I find the text within a div in the source of a web page using C#
- Web API OData media type formatter when using $expand
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- ASP.NET Web API GET Method: Passing multiple values for single parameter
- LINQ Source Code Available
- How to properly integration test Web Api controller with IEnumerable results?
- ASP.NET Web API - Entity Framework - 500 Internal Server Error On .Include(param => param.field)
- How to create an IQueryable Web API that can pull data from several data sources?
- .NET 4 Code Contracts: "requires unproven: source != null"
- ASP NET CORE Entity Framework Select with GroupBy Id
- ASP.NET Web Api join two tables to make an array of objects
- EF TPH inheritance lost in Web Api JSON
- Wrong Serialization on LINQ Web API
- Deserialize Web Api OData response
- Shuould we perform LINQ directly in asp net mvc views
- creating Linq to sqlite dbml from DbLinq source code
- Creating Dynamic Search Queries with Entity Framework Web API using passed in parameters as the search fields
- Multiple call using Web Api Service
- Adding array title to Linq-to-SQL-to-JSON in Web API
- Running different code depending on what Net Framework version is installed
More Query from same tag
- Asp.Net MVC Rollback Changes?
- Is it possible to convert a string to its ExpressionType counterpart?
- Inconsistencies in data retrieval from csv file
- nested Grouping in Linq
- How to create xml which has many namespace in it like the below
- Getting Distinct count from the list
- How to combine two types of C# lists into one?
- Better Way to Check for Null Value in Linq Path
- linq query comparing Dates within a list
- In a LINQ query, when calling ToDictionary, what is assigned the key and value?
- sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2) Using Linq and MySql
- How does one sort using Entity Framework Dynamic "SortBy" string on a complex object?
- How to cast an Collection<x> to an IQueryable<x>
- How to get list1 items that any items of sublist1 at least has one item of list2?
- What is the alternate way of using LINQ ForEach loop?
- transpose or translate Expression Linq
- Convert expression tree types
- Calculating the difference between two column with NULL's using Entity Framework
- Entity Framework 6 including only child records that have a specific value?
- Mongodb c# filter using and or operator
- Boolean method to check whether List<DateTime> are sequential at one-hour interval
- Identity Filter Linq .Where
- How can i remove an whole XML Block just by knowing an element inside it
- How do I get values from SelectedItem in ComboBox with Linq and C# 3.5
- Can I use Linq to set properties on List<object> from data reader
- Is there any way to map a ISO Currency Code back to a culture string?
- How to limit a LINQ query based on date and a secondary ID
- Dynamically setting properties in the current context
- C# Entity Framework 4 Common Language Runtime detected an invalid program error?
- OrderBy before and after Except