简体   繁体   中英

Using Join with IQueryable LINQ

I am trying to implement Inner join query on two tables opportunityProducts and Products where I am supposed to return Iqueryable element in my MVC web API service. But from below, I am not able to get result as it throws error for conversion.

public IQueryable<OpportunityProducts> GetProductsByShipID(int id)
 {
   IQueryable<OpportunityProducts> oppProductss = 
                  from c in db.OpportunityProducts
                  from p in db.Products
                  where p.ProductID == c.ProductID
                  select new { c.Quantity,c.ProductDesc,c.RemainingQuantity, p.QtyInHand};
    return oppProductss;
  }

You need to fill the Type you wish to return instead of returning an anonymous type. Here since you are querying the OpportunityProducts , I think you don't have QtyInHand property. So you can either return a new type altogether or add this property.:-

IQueryable<ResultantProducts> oppProductss = 
                  from c in db.OpportunityProducts
                  from p in db.Products
                  where p.ProductID == c.ProductID
                  select new ResultantProducts
                          { 
                               Quantity = c.Quantity,
                               ProductDesc = c.ProductDesc,
                               RemainingQuantity = c.RemainingQuantity, 
                               QtyInHand = p.QtyInHand
                          };

I see an error in your code. You should return objects of type OpportunityProducts, I mean:

    public IQueryable<OpportunityProducts> GetProductsByShipID(int id)
    {
       IQueryable<OpportunityProducts> oppProductss = from c in db.OpportunityProducts
              from p in db.Products
              where p.ProductID == c.ProductID
              select new OpportunityProducts    // <---- THIS!
              { 
                    Quantity = c.Quantity,
                    ProductDesc = c.ProductDesc,
                    RemainingQuantity = c.RemainingQuantity, 
                    QtyInHand = p.QtyInHand
              };
       return oppProductss;
    }

I hope it helps you.

Regards,

Julio

I think you can create class called ResultProducts with all properties(same data type in the original table (nullable also need to put)) what you want to get. Then you can return that object.

public class ResultProducts 
    {

            public int Quantity { get; set; }
            public string ProductDesc { get; set; }
            public int  RemainingQuantity { get; set; }
            public int QtyInHand { get; set; }
     }

public IQueryable<ResultProducts> GetProductsByShipID(int id)
        {
            var oppProductss =from c in db.OpportunityProducts
                              from p in db.Products
                              where p.ProductID == c.ProductID
                              select new ResultProducts()
                              { 
                               Quantity =c.Quantity,
                               ProductDesc= c.ProductDesc,
                               RemainingQuantity=c.RemainingQuantity,
                               QtyInHand=p.QtyInHand 
                               };

            return oppProductss ;
        }

I hope this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM