0
public IEnumerable<Temp_Order> Get_Temp(string id)
{
    //List<Temp_Order> data = new List<Temp_Order>();
    IEnumerable<Temp_Order> data = db.Temp_Order
                                      .Join(db.Items, 
                                            t_id => t_id.ItemId, 
                                            I_id => I_id.ItemId,
                                            (t_id, I_id) => new  { t_id.Quantity, I_id.ItemName })
                                      .Where(x => x.ItemName == id);

    return data;
}

In this method I want IEnumerable<Temp_Order>. So I will use this in controller and return to the view.

I'm getting this error:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) E:\WORK\Projects\RMS_Live\RMS_Live\Models\Order.cs

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

The Join is converting your query to an IEnumerable of an anonymous type. Add a Select on the end to cast is back to Temp_Order:

public IEnumerable<Temp_Order> Get_Temp(string id)
{
    //List<Temp_Order> data = new List<Temp_Order>();
    IEnumerable<Temp_Order> data = db.Temp_Order
                    .Join(db.Items, t_id => t_id.ItemId, I_id => I_id.ItemId, (t_id, I_id) => new  { t_id.Quantity, I_id.ItemName })
                    .Where(x => x.ItemName == id)
                    .Select(a => new Temp_Order
                        {
                            ItemName = a.ItemName,
                            Property2 = a.Property2,
                            //snip
                        });

    return data;
}

EDIT:

You indicate in the comments that you want all properties from both Temp_Order and Item which means you need another class. You can get away without creating another class, but it's much simpler in the long run. So first make your class, 2 ways spring to mind, you either replicate all the properties you need or just return the 2 objects, I'll use the latter:

public class OrderItem
{
    public Temp_Order Temp_Order { get; set; }
    public Item Item { get; set; }
}

Now your function becomes this:

public IEnumerable<OrderItem> Get_Temp(string id)
{
    IEnumerable<OrderItem> data = db.Temp_Order
                    .Join(db.Items, 
                          t_id => t_id.ItemId, 
                          I_id => I_id.ItemId,
                          (t_id, I_id) => new OrderItem
                              {
                                  Temp_Order = t_id,
                                  Item = I_id
                              })
                    .Where(x => x.ItemName == id);

    return data;
}

And in your view, make sure you set the model type to IEnumerable<OrderItem> and you can access all the properties like this:

@Model.Temp_Order.ItemName
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Thnx :D ... but how would i access Item table fields in Select? – ASP.Net Developer Oct 25 '14 at 18:35
  • `a` contains the fields of everything you select in the Join clause, so you only have `Quantity` and `ItemName` available. – DavidG Oct 25 '14 at 18:37
  • .Select(a => new Temp_Order { Quantity = a.Temp_Order.Quantity, ItemName=a.Item.ItemName} ... it is giving error.... only Temp_Order fields can be accessed in this SELECT :( – ASP.Net Developer Oct 25 '14 at 18:40
  • Remember you are storing all this data inside `Temp_Order` objects, you cannot add your own properties to it. – DavidG Oct 25 '14 at 18:41
  • So what should my return type be instead of Ienumerable? I want all fields of Temp_order and Item Table and display them in view.... plz help me – ASP.Net Developer Oct 25 '14 at 18:45