我的 DAL 应该返回 DataTables 还是我...无论如何<>?

发布于 2024-07-19 07:35:39 字数 2146 浏览 3 评论 0 原文

编辑 1

我很抱歉,但在阅读了 2 篇建议的文章后,我仍然不明白我应该使用什么。 我知道由于各种原因,使用 IQueryable 并不是首选,但这是否也消除了 IEnumerable ? DataTable 真的是我的最佳选择吗?

简而言之,我猜,首选的返回类型是什么?


我有以下简单的 LINQ 查询,我想将其抽象为 DAL。 var 的类型是什么,因此我的方法应该是什么类型?

            ConnectDBDataContext context = new ConnectDBDataContext();

        var lName = textEdit1.Text;

        var searchByPersonLName = from c in context.tblPersons
                                  where c.LastName == lName
                                  orderby c.LastName
                                  select new { c.FirstName,c.LastName,c.PersonID};

        dataGridView1.DataSource = searchByPersonLName;

当我将鼠标悬停在 VS 中的它上面时,它会显示 IQueryable,但是当我放入断点并运行它时,它似乎将自己称为 IEnumerable。 哪一个是正确的,我应该如何声明我的方法?

像这样-->

        public static DataTable SearchPerson(string SearhParam)
    {
        ConnectDBDataContext context = new ConnectDBDataContext();
        var persons = (from person in context.tblPersons
                       orderby person.LastName
                       select new { person.PersonID, person.LastName, person.FirstName, person.SSN });
        var filteredPersonsList = persons.Where(p => p.LastName == SearhParam).ToList();
        if (filteredPersonsList.Count == 0)
            filteredPersonsList = persons.Where(p => p.LastName.StartsWith(SearhParam)).ToList();

        var dataTable = filteredPersonsList.CopyLinqToDataTable();

        return dataTable;
    }

如果我使用 IQueryable 什么是 或者我如何知道它以及我会返回什么?

谢谢!

作为参考,CopyToDataTable() 如下。

public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source)
    {
        return new ObjectShredder<T>().Shred(source, null, null);
    }

    public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source,
                                                DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }

EDIT 1

I apologize but after reading the 2 suggested articles I still don't understand what I should use. I understand that using IQueryable is not preferred for various reasons but does that eliminate IEnumerable as well? Is a DataTable really my best option?

In short, I guess, what is the preferred Return type?


I have the following simple LINQ query that I want to abstract out into a DAL. What is the type of var and therefore what type should my method be?

            ConnectDBDataContext context = new ConnectDBDataContext();

        var lName = textEdit1.Text;

        var searchByPersonLName = from c in context.tblPersons
                                  where c.LastName == lName
                                  orderby c.LastName
                                  select new { c.FirstName,c.LastName,c.PersonID};

        dataGridView1.DataSource = searchByPersonLName;

When I hover over it in VS it says IQueryable<T> but when I put in a breakpoint and run it it seems to call itself IEnumerable. Which is correct and how should I declare my method?

Like this -->

        public static DataTable SearchPerson(string SearhParam)
    {
        ConnectDBDataContext context = new ConnectDBDataContext();
        var persons = (from person in context.tblPersons
                       orderby person.LastName
                       select new { person.PersonID, person.LastName, person.FirstName, person.SSN });
        var filteredPersonsList = persons.Where(p => p.LastName == SearhParam).ToList();
        if (filteredPersonsList.Count == 0)
            filteredPersonsList = persons.Where(p => p.LastName.StartsWith(SearhParam)).ToList();

        var dataTable = filteredPersonsList.CopyLinqToDataTable();

        return dataTable;
    }

If I use IQueryable<T> what is <T> or how do I know that and what would I return?

Thanks!

For Reference the CopyToDataTable() is below.

public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source)
    {
        return new ObjectShredder<T>().Shred(source, null, null);
    }

    public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source,
                                                DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }

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

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

发布评论

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

评论(2

停顿的约定 2024-07-26 07:35:39

首先,IQueryable 实现了 IEnumerable,因此这就是您可能会看到两者的原因。 请参阅此处了解更多详细信息

一般来说,我会推荐您的 DAL尽可能返回您的实际对象。

我会阅读此博客< /a> 了解如何做以及如何不做您所建议的事情的指南。 简短的回答,不要返回 IQueryable。

编辑:
例子:

        internal static File[] GetAllFilesByUserID(int userID)
    {
        var db = GetDataContext();
        return (from files in db.Files where files.OwnerUserID == userID select files).ToArray();
    }

First off, IQueryable implements IEnumerable, so that is why you are potentially seeing both. See here for more details

Generally, I would recommend your DAL return your actually objects whenever possible.

I would read this blog for guidelines on how to, and how not to do what you are suggesting. Short answer, don't return IQueryable.

EDIT:
Example:

        internal static File[] GetAllFilesByUserID(int userID)
    {
        var db = GetDataContext();
        return (from files in db.Files where files.OwnerUserID == userID select files).ToArray();
    }
掀纱窥君容 2024-07-26 07:35:39

他的意思是将您的数据映射到您希望 DAL 返回的对象。

在回答你的第一个问题时,“var”实际上只是变量的缩写,类型是赋值中定义的类型。

var myvariable = string.empty;

在此示例中,类型是字符串。

var myreader = new StringReader();

而在此示例中,类型是 StringReader 的类型。

至于你的第二个问题“是什么”。 T 是泛型类型。

有关 dal 将返回实际对象的示例:

 public Product GetProduct(int ProductID)
    {
        var product = from p in db.MyTable
                      where p.productID == ProductID
                      select new product { name = p.name, pricepoint = p.pricepoint, qty = p.quantity };

        return product;
    }

What he means is to map your data to the object you are wanting the DAL to return.

In answer to your first question "var" is really just short for variable, and the type is what ever type is defined in the assignment.

var myvariable = string.empty;

In this example the type is that of a string.

var myreader = new StringReader();

While in this example the type is that of a StringReader.

As for your second question of "what is ". T is a generic type.

For an example of where your dal would be returning an actual object:

 public Product GetProduct(int ProductID)
    {
        var product = from p in db.MyTable
                      where p.productID == ProductID
                      select new product { name = p.name, pricepoint = p.pricepoint, qty = p.quantity };

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