【问题标题】:Should my DAL return DataTables or I...whatever<>?我的 DAL 应该返回 DataTables 还是我......无论如何<>?
【发布时间】:2010-10-24 14:39:35
【问题描述】:

编辑 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&lt;T&gt;,但是当我放入断点并运行它时,它似乎自称 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&lt;T&gt; &lt;T&gt; 是什么,或者我怎么知道,我会返回什么?

谢谢!

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

【问题讨论】:

    标签: c# linq linq-to-sql n-tier-architecture


    【解决方案1】:

    首先,IQueryable 实现了 IEnumerable,这就是为什么您可能会同时看到这两者。见here for more details

    一般来说,我建议您的 DAL 尽可能返回您的实际对象。

    我会 read this blog 获取有关如何执行以及如何不执行您的建议的指导。简短的回答,不要返回 IQueryable。

    编辑: 示例:

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

    【讨论】:

    • 谢谢,“实际对象”是什么意思 你能给我一个简单的例子吗?
    • Se Davids 的帖子,它解释了我的意思。通常我使用 L2S 创建的类,或者其中的一个数组。我将编辑一个示例,说明我的 DAL 如何从我的 FileLibrary 返回 File 对象。
    【解决方案2】:

    他的意思是将您的数据映射到您希望 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 2011-03-03
      • 1970-01-01
      • 2014-08-31
      • 2012-04-27
      • 2019-10-01
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多