score:1
First, I'm assuming your DTO is meant to contain public string SaleDepartmentDescription { get; set; }
as your question refers to it but it isn't actually there.
If you are NOT using EF migrations (a fair assumption since otherwise you'd just add the foreign key!), then you can do this by adding keys in your Entities - the keys don't actually need to present in the database for EF to join on them, this just tells EF to pretend that they are. (If you are using EF migrations then this approach will not work, as it will want to add the keys to the DB.)
public class Consumable
{
public int ConsumableId { get; set; }
public string Description { get; set; }
public int SaleDepartmentId { get; set; }
[ForeignKey("SaleDepartmentId")]
public virtual SaleDepartment SaleDepartment { get; set; }
}
Assuming your DTO does contain the string property SaleDepartmentDescription
then AutoMapper will handle this automatically, though you should use ProjectTo to make more efficient database queries:
var mappedDTOs = context.Consumable.ProjectTo<ConsumableDTO>().ToList();
Source: stackoverflow.com
Related Articles
- How to ask Automapper to grab related record by inner join on Id field which is not a foreign key?
- Linq How to get the master record and a specific field of inner details record
- LINQ, GroupBy inner value of field which has type of object
- Record just created in Model has null related table instead of empty which breaks my query?
- How to Insert data in Join table which were created by using EF Code First
- join table and get the record which has minimum value?
- Which kind of join should i use? [EF4.0 code first ]
- LINQ Inner Join - Return one object from anonymous which is combined from Both Tables
- How do you left join in Linq if there is more than one field in the join?
- How to inner join tables from different Data Context?
- C# Linq Inner Join
- Include and Where predicate cause left join instead of inner join
- LINQ to SQL join generates SQL which joins on IS NULL
- LINQ to Entities Join on Nullable Field where Null Implies "Match All"
- Left outer join using LINQ -- understanding the code
- linq join query get single record from second table
- SQL INNER JOIN vs Where Exists Performance Consideration
- NHibernate 3 LINQ inner join issue with three jumps: NotSupportedException
- LINQ Source Code Available
- MVC EF - Inner Join
- SQL: Inner Join return one row based on criteria
- Get only one (last) record in one-to-many join with linq-to-entities
- EF Include always produces INNER JOIN for the first Navigation Property
- LINQ Method Syntax with INNER and OUTER Join
- double inner join in linq/ lambda query?
- Inner Join Using Lambda (LINQ)
- .NET 4 Code Contracts: "requires unproven: source != null"
- LINQ inner join
- Linq group by on multiple table and inner join
- linq join 1 to many get first record
- OrderBy().ThenBy().ThenBy() not giving expected result on a List of entities
- Where do I put the "orderby group.key" in this LINQ statement?
- linq get multiple values at certain conditions
- Comparing Two lists of Different Objects without for loop
- How to update an XML node with LINQ?
- Select unique items with LINQ
- Concatenation of measurements in SQL Database with Linq
- LINQ with Entity Framework - basic question
- C# Nullable<T> query comprehension - "expression is always true" warning
- ASP.NET xml linq check if element exist
- How to convert MethodCallExpression to BinaryExpression
- Create anonymous type by selector expressions
- Unable to cast object of type 'ImagesFlag' to type 'System.Collections.Generic.IEnumerable`1[ImagesFlag]'
- Find records that contains at least one word of a string
- C# Dynamic Linq Variable Where clause
- Get a random item from a collection that does not already exist in another collection - LINQ?
- LINQ avoid specific grouping
- Using ToList ForEach in an ActionResult to update table values
- List<T> to implement IQueryable<T>
- LINQ join with multiple conditions of different kind