Craig
Craig

Reputation: 1225

LINQ Retrieve many to one relationship database

I currently have the follow LINQ code working which returns one row per quote. I need to modify this to return an additional table that could have many records per quote and I am not sure how to add this bit of code as I am still learning.

    var query = DbContext.QuoteInformation
                .Include("Customer")
                .Include("Priority")
                .Include("Status")
                .OrderBy(q => q.RFQ);

    totalRecords = query.Count();

    return query.ToList();

The table that I need to include in this query has these columns -

public class QuoteTransaction
{
    [Key]
    public Guid Id { get; set; }
    public Guid QuoteInformationId { get; set; }
    public Guid DepartmentId { get; set; }
    public Guid StatusId { get; set; }
    public bool Checked { get; set; }
    public string Comments { get; set; }
    public string UserId { get; set; }

    public QuoteInformation QuoteInformation { get; set; }
    public Department Department { get; set; }
    public Status Status { get; set; }
    public ApplicationUser User { get; set; }

    public QuoteTransaction()
    {
        Id = SequentialGuid.Create(SequentialGuidType.SequentialAtEnd);
    }
}

Upvotes: 1

Views: 123

Answers (1)

Jahirul Islam Bhuiyan
Jahirul Islam Bhuiyan

Reputation: 799

You need to add a property

public virtual List<QuoteTransaction> QuoteTransactions{ get; set; }

into class QuoteInformation to get list of tansactions

and then change your LINQ with .Include("QuoteTransactions")

You can also include by adding following code in the LINQ if it return navigation property exception

.Include(r=>r.QuoteTransactions) 

Upvotes: 1

Related Questions