0

I'm trying to retrieve company with trades. I have included the entites and the retrieve method. As shown, the companies are inside trades already.

//company entity
public class Companies
{
  public int id { get; set; }
  public string companyID { get; set; }
  public string companyName { get; set; }
  public bool companyMointor { get; set; }   
}

//trade entity
public class Trade
{
  public int id { get; set; }
  public DateTime tradeDate { get; set; }
  public double tradePrice { get; set; }
  public int tradeQuantity  { get; set; }
  public Companies tradeCompany { get; set; }
  public int type { get; set; }

  public types tradeType 
  {
    get { return (Entities.types)type; }
    set { type = (int) value; }
  }
}

//methods to retrieve
public List<Trade> getTrade()
{
  List<Trade> trades = (from t in dbContext.trades
                        orderby t.tradeDate descending             
                        select t).ToList();
  return trades;
}
1
  • What is your problem, is the company variable empty? in the trades list? Commented Nov 22, 2011 at 14:22

2 Answers 2

1

Use the Include method to eager load navigational properties.

public List<Trade> getTrade()
{
    List<Trade> trades=dbContext.trades.Include(t => t.tradeCompany)
      .OrderBy(t => t.tradeDate).ToList();

    return trades;
}

Edit: If you are using ObjectContext API try the following

public List<Trade> getTrade()
{
    List<Trade> trades=dbContext.trades.Include("tradeCompany")
      .OrderBy(t => t.tradeDate).ToList();

    return trades;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Cannot convert lambda expression to type 'string' because it is not a delegate type
A specified Include path is not valid. The EntityType 'Shares.Entities.Trade' does not declare a navigation property with the name 'Companies'.
@user974459 Did you replace tradeCompany parameter with Companies?
@user974459 that is the problem. You need to give the name of the navigational property as a string. In your code you have named it as tradeCompany. So use that.
The underlying provider failed on Open.
|
1

change your gettrade method to have an include line:

//methods to retrieve 
    public List<Trade> getTrade() 
    { 
        List<Trade> trades=(from t in dbContext.trades 
                .Include("tradeCompany")
                orderby t.tradeDate descending 

                select t).ToList(); 
        return trades; 
    } 

1 Comment

A specified Include path is not valid. The EntityType 'Shares.Entities.Trade' does not declare a navigation property with the name 'Companies'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.