非静态方法需要目标C#实体框架

时间:2016-07-29 11:30:08

标签: c# entity-framework linq

public InventorySales  getinvSales(int mgrId, int SalesId)
{
    Managerset res = null;


    InventorySales invSales = (from x in sbdb.tblSalesn.where(i => i.salesId == Sid)
                               join y in sbdb.tblProds on x.salesid equals y.salesid into resSales

     select new InventorySales()
                                      {
                                           ProductName = x.productname,
                                           Location = (xyz!= null) ? xyz.location:string.empty,
                                           Manager =  (res != null) ? res.Manager : string.empty // error line
                                      }).FirstOrDefault();

    return invSales
}

这就是我的代码的外观,

Manager = (res != null) ? res.Manager : string.empty 

导致错误,tblManager和tblSales表之间没有关系

1 个答案:

答案 0 :(得分:1)

您可以尝试将res != null决定移出查询。

public InventorySales  getinvSales(int mgrId, int SalesId) {
    var res = from ins in sbdb.tblManager
          where(managerId == mgrID)
          select new Managerset() {
            Manager = ins.Manager
          }).firstOrDefault();

    InventorySales invSales = (from x in sbdb.tblSalesn.where(i=>i.salesId == Sid)
                                join y in sbdb.tblProds on x.salesid equals y.salesid into resSales
                                from xyz in result.DefaultIfEmpty()
                                select new InventorySales()
                                {
                                    ProductName = x.productname,
                                    Location = (xyz!= null) ? xyz.location:string.empty,
                                    Manager =  string.empty
                                }).FirstOrDefault();

    if (invSales != null && res != null) {
        invSales.Manager = res.Manager;
    }

    return invSales
}