实体框架,通用列表

发布于 2025-01-08 07:24:37 字数 959 浏览 3 评论 0原文

我对通用列表有疑问。

假设我有一个数据库,其中包含三个表:用户表、订单表和项目表。

在 C# 代码中,我想要一个名为 GetList 的函数来从三个表之一获取所有记录。

如果我不使用通用方法,我必须创建 3 种不同的方法,例如 GetAllUsers()、GetAllOrders() 和 GetAllItems()。在这 3 个方法中,我必须编写 linq 来查询数据库。

我的问题是如何实现通用方法。

我想使用 EF 4.1。我知道如何使用 Nibrate 来做到这一点。

更新

这怎么样?我从另一篇文章中找到它,它可以用来获取单个记录

        public T GetSingleEntity<T>(Expression<Func<T,bool>> expression) where T:class
    {
        using (PersonalLinksEntities dbContext = new PersonalLinksEntities()) 
        {
            return dbContext.CreateObjectSet<T>().SingleOrDefault(expression);
        }

    }

PersonalLinksEntities 是由 EF 生成的,它是数据库优先模型。

这是我如何调用此方法

    public ActionResult Index()
    {
        var thisOne = base.GetSingleEntity<PersonalRecord>(r=>r.RecordID==1);            
        return View();
    }

I have a question about generic list.

Assume that I have a database which contains three tables, User table, Order table and Item table.

In the C# code, I want a function, called GetList to get all he records from one of the three tables.

If I don't use generic method, I have to create 3 different methods which looks like GetAllUsers(), GetAllOrders() and GetAllItems(). Inside these 3 methods, I will have to write the linq to query the database.

My question is how do I implement a generic method.

I want to use EF 4.1. I know how to do it by using NHibrate tho.

UPDATE

What about this? I found it from another post, it can be used to get one single record

        public T GetSingleEntity<T>(Expression<Func<T,bool>> expression) where T:class
    {
        using (PersonalLinksEntities dbContext = new PersonalLinksEntities()) 
        {
            return dbContext.CreateObjectSet<T>().SingleOrDefault(expression);
        }

    }

PersonalLinksEntities is generated by EF, it is database first model.

Here is how I call this method

    public ActionResult Index()
    {
        var thisOne = base.GetSingleEntity<PersonalRecord>(r=>r.RecordID==1);            
        return View();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2025-01-15 07:24:37

具体实现取决于您用来访问数据的方式。 NHib、EF、L2S?这些都有一些通用的访问 IQueryable 的方法。

您可以公开如下方法:

public IQueryable<T> GetQueryable<T>()
{
   //Implementation depends on your data provider
   //return dataContext.GetTable<T>();
   //return session.Query<T>();
}

但您可能想要做的是遵循存储库模式,为每种类型使用不同的存储库。但是您可以使用接受类型参数并且可重用的抽象基础存储库。

public abstract class RepositoryBase<T>
{
   public IQueryable<T> GetQuery()
   {
     //Implementation determined by provider
    }

}

The specific implementation going to depend on what you are using to access your data. NHib, EF, L2S? These all have some way of accessing an IQueryable generically.

You could expose a method such as:

public IQueryable<T> GetQueryable<T>()
{
   //Implementation depends on your data provider
   //return dataContext.GetTable<T>();
   //return session.Query<T>();
}

But probably what you want to do is follow a repository pattern with a different repository for each type. But you can use an abstract base repository that accepts a type parameter and is reusable.

public abstract class RepositoryBase<T>
{
   public IQueryable<T> GetQuery()
   {
     //Implementation determined by provider
    }

}
2025-01-15 07:24:37

因为问题中没有关于您已经写过的内容的详细信息。如果您只想从表中获取所有记录...为什么使用 LINQ...?您可以简单地在方法中使用数据集和数据适配器查询数据库,并使其通用,您只需传递表名称并返回数据集,之后您可以根据需要使用该数据集。如果我在这种情况下缺少任何特定的复杂性,请详细说明。

As there are no details in the question regarding what you have written already. If you just want to GET ALL RECORDS from the tables...Why LINQ...?? You can simply query the database using Data-set and Data-adapter in a method and to make that generic you just pass the table name and get back a Data-set, after that you can play with that Data-set as you like. Please elaborate more if there is any specific complexity in this case which I am missing.

孤檠 2025-01-15 07:24:37

DbContext 上已经存在您想要的称为 Set 的方法,例如 var results = dbContext.Set();

如果您使用的是 ObjectContext,那么您需要的方法是 CreateObjectSet()。

There already exists the method you want on DbContext called Set, e.g. var results = dbContext.Set();

If you are using ObjectContext then the method you want is CreateObjectSet().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文