1

I have two tables UU_DeliveryCharges and SC_Countries and I want to return result set as a join of these two in my odata function but getting an exception:

The entity or complex type 'ULYXModel.UU_DeliveryCharges' cannot be constructed in a LINQ to Entities query.

I am new in oData, please help.

oData Web API:

[EnableQuery] 
public IQueryable<UU_DeliveryCharges> GetUU_DeliveryCharges()
{
    //retrun db.UU_DeliveryCharges;

    var results = from deliveries in db.UU_DeliveryCharges                        
        join countries in db.SC_Countries on deliveries.CountryId equals countries.CountryId
        select new UU_DeliveryCharges
        {
            FlatRate = deliveries.FlatRate,
            MileRate = deliveries.MileRate,
            PickUpFee = deliveries.PickUpFee,
            DropOffFee = deliveries.DropOffFee,
            CountryName = countries.CountryName
        };

    return results;
}

Model:

public partial class UU_DeliveryCharges
{
    public int DeliveryChargeId { get; set; }     

    public Nullable<int> CountryId { get; set; }      

    public Nullable<decimal> MileRate { get; set; }

    public Nullable<decimal> FlatRate { get; set; }          

    public Nullable<decimal> PickUpFee { get; set; }

    public Nullable<decimal> DropOffFee { get; set; }           

    public string CountryName { get; set; }            
}  

public partial class SC_Countries
{
    public int CountryId { get; set; }

    public string CountryName { get; set; }           
}
ChW
  • 3,168
  • 2
  • 21
  • 34
Mona
  • 21
  • 5
  • You need to materialize the query to memory first, before mapping to the model. –  Apr 07 '18 at 08:11
  • Possible duplicate of [The entity cannot be constructed in a LINQ to Entities query](https://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query) –  Apr 07 '18 at 08:12
  • I'm not clear @StephenMuecke, can u pl explain more.. – Mona Apr 09 '18 at 04:53
  • And in your referenced example resultset is in toList() but I want IQueryable. Isn't is possible to do so? – Mona Apr 09 '18 at 05:01
  • Read the answers in the dupe - you cannot project the query into the same type (but you model makes no sense anyway - `UU_DeliveryCharges` already contains a property which is `string CountryName` –  Apr 09 '18 at 05:03

0 Answers0