Return 来自另一个 class 实例的 IQueryable<T>

Return IQueryable<T> from another class instance

我想要完成的是制作一个 class 的基础 return 通用 IQueryable<T> 并使用另一个 class 的 IQueryable ] 实例进一步过滤。 Es。 基本 class return 所有记录,调用 class 从那里过滤。

这是基础 class,getDealer_query 是 return 主要的方法 IQueryable

 public class Dealer
 {
    public DAL.DBConn_Nav db = null;
    public G4.Elements.Identity oIdentity = null;

    public Dealer()
    {
        oIdentity = new G4.Elements.Identity();
        oIdentity.read();
    }

    public IQueryable<outerViewModel.NavisionDAL.vmDealer> getDealer_query()
    {
        IQueryable<vmDealer> dealer = (from d in db.DENTALICA_S_P_A__Customer
                                       select new vmDealer()
                                       {
                                           idDealer = d.No_,
                                           name = d.Name + (d.Name_2.Length > 0 ? " " + d.Name_2 : ""),
                                           address = d.Address + (d.Address_2.Length > 0 ? " " + d.Address_2 : ""),
                                           city = d.City,
                                           zip = d.Post_Code,
                                           county = d.County,
                                           email = d.E_Mail,
                                           phone = d.Phone_No_,
                                           fax = d.Fax_No_,
                                           vat = d.VAT_Registration_No_,
                                           ssn = d.Fiscal_Code,
                                           businessAreaCode = d.Business_Area_Code,
                                           businessZoneCode = d.Business_Zone_Code,
                                           clienteDiretto = (d.Cliente_Diretto == (byte)1 ? true : false),
                                           aggancioOrdiniWeb = (d.Deposito_Agg__Ordini_Web == (byte)1 ? true : false),
                                           aggancioOrdiniG4 = (d.Deposito_Agg__Ordini_G4 == (byte)1 ? true : false),
                                           notUse = (d.Not_Use == (byte)1 ? true : false),
                                           blocked = d.Blocked,
                                           shipmentMethodCode = d.Shipment_Method_Code,
                                           paymentTermsCode = d.Payment_Terms_Code,
                                           paymentMethodCode = d.Payment_Method_Code,
                                           shippingAgentCode = d.Shipping_Agent_Code,
                                           customerPriceGroup = d.Customer_Price_Group,
                                           customerDiscountGroup = d.Customer_Disc__Group,
                                           identificaCliente = d.Identifica_Cliente

                                       });
        return dealer;
    }

这是另一个 Class CustomerDealer 的实例调用方法并最终列出数据。

public class Customer
{
    G4.Elements.Identity oIdentity = null;
    DAL.DBConn_Nav navDB = null;

    public Customer()
    {
        //oIdentity = new G4.Elements.Identity();
        //oIdentity.read();
    }

    ///// <summary>
    ///// Recupera l'elenco dei clienti Navision (i Depositi pr G4) visibili ad un particolare utente
    ///// </summary>
    ///// <returns></returns>
    public IQueryable<outerViewModel.NavisionDAL.vmDealer> getCustomerForOperator_query()
    {
        string idOperator = !string.IsNullOrEmpty(oIdentity.selectedId) ? oIdentity.selectedId : oIdentity.id;

        using (navDB = new DAL.DBConn_Nav()) {
            G4.Models.NavisionDAL.Dealer oD = new G4.Models.NavisionDAL.Dealer();
            // Tutti i Clienti
            IQueryable<outerViewModel.NavisionDAL.vmDealer> allCustomerList_query = oD.getDealer_query();
            IQueryable<outerViewModel.NavisionDAL.vmDealer> customerForOperator_query = (from c in allCustomerList_query                                                                                join cForOp in navDB.DENTALICA_S_P_A__SalesPerson_Customer on c.idDealer equals cForOp.Customer_No_
                                                                                         where cForOp.Salesperson_Code.Equals(idOperator)
                                                                                         && cForOp.Starting_Date < DateTime.Now
                                                                                         && (cForOp.Ending_Date.Equals(new DateTime(1753, 1, 1)) || cForOp.Ending_Date > DateTime.Now)
                                                                                         && c.notUse.Equals(false)
                                                                                      select c).Distinct();

            return customerForOperator_query;
        }
    }

    public List<G4.outerViewModel.NavisionDAL.vmDealer> getCustomerForOperator()
    {
        using (navDB = new DAL.DBConn_Nav())
        {
            G4.Models.NavisionDAL.Dealer oD = new G4.Models.NavisionDAL.Dealer();
            return getCustomerForOperator_query().ToList();
        }
    }

但是没用!我得到一个通用的 "Object not set to an instance...".

即使我尝试调试也无处可去,因为光标不会停止。

为了让它工作,我必须从客户那里继承经销商 class,但这不是我想要的。

我想我在这里遗漏了一些基本的东西。

有什么想法吗?

编辑 - 简化样本 我正在添加相同代码的简化示例。

public class Dealer1
{

    public DAL.DBConn_Nav db = null;

    public Dealer1() { 

    }

    public IQueryable<outerViewModel.NavisionDAL.vmDealer> getDealer_query()
    {

        IQueryable<vmDealer> dealer = (from d in db.DENTALICA_S_P_A__Customer

                                       select new vmDealer()
                                       {
                                           idDealer = d.No_,
                                           name = d.Name + (d.Name_2.Length > 0 ? " " + d.Name_2 : "")

                                       });

        return dealer;

    }
}


 public class Customer1
{

    public DAL.DBConn_Nav db = null;

    public Customer1()
    {

    }

    public IQueryable<outerViewModel.NavisionDAL.vmDealer> getCustomerForOperator_query()
    {

        // All customers
        G4.Models.NavisionDAL.Dealer1 oD = new G4.Models.NavisionDAL.Dealer1();

        IQueryable<outerViewModel.NavisionDAL.vmDealer> allCustomerList_query = oD.getDealer_query().Where(i => i.name.StartsWith("AAA"));

        return allCustomerList_query;

    }

    public List<G4.outerViewModel.NavisionDAL.vmDealer> getCustomerForOperator()
    {

        using (db = new DAL.DBConn_Nav())
        {
            return getCustomerForOperator_query().ToList();
        }


    }
}

allCustomerList_query 不应该是空的。

为确认 getDealer_query 方法中的 linq 语句正常工作,我会将您的代码更改为如下所示:

        List<outerViewModel.NavisionDAL.vmDealer> something = new List<outerViewModel.NavisionDAL.vmDealer>();
        IQueryable<outerViewModel.NavisionDAL.vmDealer> test = something.AsQueryable();
        return test;

无论如何,为了防止对象引用错误,你需要保证返回一个实例,从代码示例中,我实际上无法分辨。

奇怪的是在getDealer_query方法内部无法获取断点停止。您 运行 这是在多线程测试中 运行 吗?可能发生的情况是另一个线程引发了未处理的异常,因此整个测试停止了。尝试在 getDealer_query 中的 return dealer 之前和行 G4.Models.NavisionDAL.Dealer oD = new G4.Models.NavisionDAL.Dealer(); 上放置断点。假设没有其他线程,代码必须在其中一个线程上停止。

最后一点,查看代码,我没有看到实例化 db 的位置。如果未实例化,您将在此行收到对象引用错误:

    `IQueryable<vmDealer> dealer = (from d in db.DENTALICA_S_P_A__Customer`

您的 class Dealer 中没有设置 db 实例的任何内容。

public DAL.DBConn_Nav db = null;

所以即使在 Customer 中你调用 using (navDB = new DAL.DBConn_Nav()) 你没有将 navDB 传递给 Dealer 所以当你调用 getDealer_query() 时你得到一个空引用异常。


根据您的简化示例,我推荐此代码:

public class Dealer1
{
    internal IQueryable<vmDealer> getDealer_query(DAL.DBConn_Nav db)
    {
        return
            from d in db.DENTALICA_S_P_A__Customer
            select new vmDealer()
            {
                idDealer = d.No_,
                name = String.Join(" ", d.Name, d.Name_2)
            };
    }
}

public class Customer1
{
    internal IQueryable<vmDealer> getCustomerForOperator_query(DAL.DBConn_Nav db)
    {
        return new Dealer1().getDealer_query(db).Where(i => i.name.StartsWith("AAA"));
    }

    public List<vmDealer> getCustomerForOperator()
    {
        using (var db = new DAL.DBConn_Nav())
        {
            return getCustomerForOperator_query(db).ToList();
        }
    }
}

这清楚地允许您分离责任,但它让您完全控制 db 实例 - 完成后您需要处理它。