返回IQueryable<;T>;来自另一个类实例

本文关键字:另一个 实例 IQueryable lt 返回 gt | 更新日期: 2023-09-27 18:29:23

我试图实现的是创建一个返回通用IQueryable<T>的基类,并使用另一个类实例中的IQueryable进行进一步筛选。Es。基类返回所有记录,调用类从此开始过滤

这是基类,getDealer_query是返回主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;
    }

这是另一个类Customer,它从Dealer的实例调用方法,并最终列出数据。

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 a instance…"。

即使我尝试调试,我也不会得到任何结果,因为光标不会停止。

为了让它发挥作用,我必须从客户那里继承Dealer类,但这不是我想要的。

我想我错过了一些基本的东西。

有什么想法吗?

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

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();
        }

    }
}

返回IQueryable<;T>;来自另一个类实例

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实例——完成后需要处理它。

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_queryreturn dealer之前和G4.Models.NavisionDAL.Dealer oD = new G4.Models.NavisionDAL.Dealer();线上放置断点。假设没有其他线程,代码必须在其中一个线程上停止。

最后要注意的是,查看代码,我看不出db在哪里实例化。如果它没有被实例化,你会在这行得到一个对象引用错误:

    `IQueryable<vmDealer> dealer = (from d in db.DENTALICA_S_P_A__Customer`
相关文章: