score:2
Accepted answer
in this case there is no reason to get a list in memory and then do the projection, you can do this directly from ef instead. even if there is no relationship defined ef will return null
for categoryname
if you project the the results. if you go to memory first then an nre is expected if there is no category relationship.
public list<documentviewmodel> all()
{
return _context.document.select(x => new documentviewmodel
{ documentid = x.documentid,
documentpath = x.documentpath,
categoryid = x.categoryid,
categoryname = x.category.categoryname}).tolist();
}
original reason why it is failing.
- there is at least one entity that does not have a corresponding relationship with category.
- you do not have lazy loading enabled (which is a good thing) and if that is the case you should use
include
to return the relationship.
Source: stackoverflow.com
Related Query
- asp.net - LINQ Query with relational Data
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LinQ query with multiple tables and extracting data
- LINQ Query to retrieve multiple levels of relational data
- Use a linq query as microsoft local report Data Source (WinForms)
- Populate Excel with data from LINQ to SQL query
- LINQ join query with relational entities
- Retrieve and print data from dynamic sql query with linq
- Query dynamic data with LINQ
- Grouping data with a LINQ query
- Linq sub query when using a repository pattern with EF code first
- Linq to sql as object data source - designer problem with partial classes
- Using LINQ query result for data source for GridControl c#
- How to swap the data source associated with a Linq query?
- Multiple data query with LINQ
- Asp net MVC 5 and listbox with linq
- Kendo Paging with Linq to SQL Classes, Query All Data Very Slow
- Avoiding repeating code with Linq query + optional params
- LINQ query finding object with closest data variable to query input variable
- Linq query for distinct data from a table with where condition
- Data duplication in DataGrid. Problem with LINQ query
- c# linq query on data table to select all minimum values with duplicates
- Conditional LINQ query on self-joining table with 2 sets of data
- Linq query for updating list of objects with some data from another list of objects
- Linq query null check in data source
- Linq query Join objects with different data sources?
- how to write a Linq query with a EF code first Many to Many relationship
- Linq Data Source WhereParameters with "\" in value
- Linq query for EF 4.1 data with nested lists bound to WPF form
More Query from same tag
- LINQ-EF Query grouping multiple tables
- EF6 query building to get the first object in the list
- Largest sub-group in a list, but deterministic
- How to authorize a user to only see his own records with asp.net Identity 2.0
- Linq query with left join having multiple tables
- For vs. Linq - Performance vs. Future
- Linq to sql stored proc not returning results
- How to write C# LINQ code to select based on condition
- How do I update a table with LINQ-to-SQL without having to delete all the existing records?
- Return Count() days to expire days
- C# return a single list into nested JSON API
- Where clause in LINQ query is not recognized in vb.net?
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
- Understanding lazy evaluation in LINQ in C#
- LINQ - using a query expression to calculate data time difference
- Is it safe to join a table twice in the same query?
- LinQ query group the same table twice
- C# LINQ to SQL - one to many relationship
- Check that all items of IEnumerable<T?> has the same value using LINQ
- Unable to get record from list by specific string value
- self referential join using linq
- Expression.Call GroupBy then Select and Count()?
- Orderby() not ordering numbers correctly c#
- Fetch selected items from Two Sided Multi Select Control
- How to get only unique nodes from a collection - IEnumerable<XNode>
- Is there a LINQ library for C++?
- Could not format node 'ClientQuery' for execution as SQL
- Filtered tokenised search result with Linq and EF
- entity exception was unhandled by user code
- getting at a deep property for a lambda Expression