【问题标题】:How to use Func with Entity Data Context?如何将 Func 与实体数据上下文一起使用?
【发布时间】:2010-08-22 17:22:58
【问题描述】:

我在下面编写的代码 MyCustomer.cs 必须返回 List ,它是 Customer 实体的类型。 © 使用了 Func 方法。我想这样做:

return erpEntityCtx.Customer.Select(select).ToList<TResult>();

但错误返回给我:

有一些无效的参数。

我可以用这个:

return erpEntityCtx.Customer.Select(c=>c.Name).ToList<TResult>(); 

但是我不能用 DataContext 做到这一点我该怎么做?

public class MyCustomer :  Manager.ILoad
    {


        #region ILoad Members


        public List<TResult> Load<TKey, TResult>(Func<TKey, TResult> select)
        {
            using (Erp.DAL.ErpEntities erpEntityCtx = new Erp.DAL.ErpEntities())
            {
                return erpEntityCtx.Customer.Select(select).ToList<TResult>();
            }
        }

        #endregion
    }



namespace Erp.BLL.Manager
{

    public interface ILoad
    {
        List<TResult> Load<TKey,TResult>(List<TKey> list, Func<TKey, TResult> select);
        List<TResult> Load<TKey, TResult>(Func<TKey, TResult> select);
    }


    public interface IErpManager
    {
        List<TResult> Load<TKey,TResult>(ILoad erpObj, List<TKey> list, Func<TKey, TResult> select);
        List<TResult> Load<TKey, TResult>(ILoad erpObj, Func<TKey, TResult> select);

    }

    public class ErpManager : IErpManager
    {

        #region IErpManager Members

        public List<TResult> Load<TKey, TResult>(ILoad erpObj, List<TKey> list, Func<TKey, TResult> select)
        {
            return erpObj.Load(list, select);
        }

        #endregion

        #region IErpManager Members


        public List<TResult> Load<TKey, TResult>(ILoad erpObj, Func<TKey, TResult> select)
        {
            return erpObj.Load(select);
        }

        #endregion
    }



}

【问题讨论】:

    标签: c# .net visual-studio linq entity-framework


    【解决方案1】:

    我认为您最初的问题之一是您的 ILoad 接口是非泛型的,但是您指定了没有泛型约束的方法,例如

    List<TResult> Load<TKey, TResult>(Func<TKey, TResult> select);
    

    首先,谓词必须评估为真或假,所以它应该真的是Func&lt;TModel, bool&gt; select,其中TModel 是您的实体类型。

    然后你正在尝试这样做:

    List<TResult> Load<TKey, TResult>(Func<TKey, TResult> select)
    {
            using (Erp.DAL.ErpEntities erpEntityCtx = new Erp.DAL.ErpEntities())
            {
                return erpEntityCtx.Customer.Select(select).ToList<TResult>();
            }
    }
    

    如果不对泛型方法(或者更好的是泛型类)指定任何约束,就无法通过 Func 满足 Func。

    您可能需要更改为:

    public interface IRepository<TModel>
    {
       List<TModel> Get(Func<TModel, bool> predicate);
    }
    

    并实现为:

    public class CustomerRepository : IRepository<Customer>
    {
        public List<Customer> Get(Func<Customer, bool> predicate)
        {
            using (var context = new ErpEntities())
            {
                 return context.Customers.Where(predicate).ToList();
            }
        }
    }
    

    您应该阅读存储库模式。

    【讨论】:

    • 但我该怎么做: public List Get(IRepository erpObj, Func predicate) { return erpObj.Get(predicate); }
    • 我想从我的问题中查看上方的经理类进行控制?
    • 为什么需要在上面继续分层?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2014-09-23
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多