我的dal返回数据表应该是我...什么<>?[英] Should my DAL return DataTables or I...whatever<>?

本文是小编为大家收集整理的关于我的dal返回数据表应该是我...什么<>?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

编辑1

我深表歉意,但是在阅读了两篇建议的文章后,我仍然不明白我应该使用什么.我知道,出于各种原因,使用Iquyable不是首选,但是这也可以消除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<T>,但是当我放入断点并运行它时,它似乎 称自己为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<T>什么是<T>或我怎么知道,我将返回什么?

谢谢!

用于参考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);
    }

推荐答案

他的意思是将您的数据映射到您要返回的对象.

在回答您的第一个问题时," 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;
    }

其他推荐答案

首先,Iquyable实现了iEnumerable,因此这就是为什么您可能会看到两者的原因.请参阅在这里有关更多详细信息

通常,我建议您尽可能返回您的实际对象.

我会此博客有关如何进行的指南以及如何不做您建议的事情.简短的答案,请勿返回iQueryable.

编辑: 示例:

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

本文地址:https://www.itbaoku.cn/post/1556903.html

问题描述

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

推荐答案

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

其他推荐答案

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