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 Query
- 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
- How to Insert data in Join table which were created by using EF Code First
- 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?
- How can I code an outer join using LINQ and EF6?
- How to check that which properties/entries are modified and which are not on Editing a record in EF 6
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- How to group a list so that a single record for a day on a DateTime field is retrived using Linq?
- How to write SQL SELECT INNER JOIN with multiple conditions (with LIKE) query to LINQ to SQL
- How can I force LINQ to SQL to perform an INNER JOIN on a nullable foreign key?
- How to do a LINQ join that behaves exactly like a physical database inner join?
- How to translate this inner join T-SQL into LINQ-to-Entities?
- How would one do an inner join in linq to entities?
- How to make a list from multiple rows from inner join in Entity Framework Core?
- How can Entity Framework be forced to generate a SQL Inner Join on a nullable Foreign Key?
- How do i delete Multiple Row of Record in this code
- LINQ, GroupBy inner value of field which has type of object
- How to convert sql inner join query into linq to sql query and convert into list
- C# Expressions: How is this AutoMapper code working?
- How to join two datasource which return all rows and group by condition?
- How to write predicate for union join and inner queries?
- How can I access the PayTime field which is inside 'paid' class in Linq
- How to write a LINQ query with inner join conditions and IN sub query
- How to perform a left join when joined field is null or 0
- How to do a inner join using LINQ
- How to use Linq join on non key field
- How to return list of records along with latest of related record using LINQ
- How can I convert HQL inner join to LINQ join
- How to convert a query consisting of INNER JOIN, LEFT JOIN and GROUP BY to a similar linq2sql query?
More Query from same tag
- Adding Summed columns together using Linq
- Linq: Join on multiple kinds of conditions
- In search of a simpler approach - and something easier on the eyes
- ASP.NET how to get the currently logged in users name and ID from a SQL database
- How can I order an IQueryable based on a field that is in another table?
- Edit multiple descendants of an element using Linq
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable
- linq date format won't go into list
- Get a count of elements that appear once in a list with LINQ
- LINQ (or) Using processing in loop? What to choose?
- LINQ query operator for log table
- Static C# variable, throw an error because of a database connection
- how to modify or insert where into expression tree
- How can I user LINQ to get a IList<string> from a multi-table join?
- Can't cast 'Newtonsoft.Json.Linq.JObject' to actual type
- Linq - Query a query result agains a list of strings
- How to get all parents for a given child node?
- How to include child entities (navigation) while using .AsExpandable() of LinqKIT
- Select many duplicating results
- Silverlight: Empty Data Grid
- LINQ select with more than one condition using OR
- Filtering the LoadWith Results
- Display and Search the specific record in ASP.NET MVC
- Deserializing json using asp.net libraries
- Confused on c# lambda expression with Linq
- Linq equivalent for collection contains at least x items; like .Any() but instead .AtLeast(int)
- How to select a rectangle from List<Rectangle[]> with Linq
- How to specify generic constraint: class inherited from interface?
- Linq to sql truncating string returned by Stored Procedure
- Getting Sum of Multiple Columns and summing up values for the same id from a list