我的DAL应该返回DataTables还是我...不管什么? [英] Should my DAL return DataTables or I...whatever<>?

查看:71
本文介绍了我的DAL应该返回DataTables还是我...不管什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,但是在阅读了2条建议的文章后,我仍然不明白我应该使用什么.我了解使用IQueryable并非出于各种原因而被优选,但是这是否也消除了IEnumerable? DataTable真的是我最好的选择吗?

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?

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

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;

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

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?

赞--

        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>或我怎么知道以及我将返回什么?

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

谢谢!

下面是CopyToDataTable()的参考.

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

推荐答案

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

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

在回答第一个问题时,"var"实际上只是变量的缩写,而类型就是赋值中定义的类型.

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

在此示例中,类型是StringReader的类型.

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

关于第二个问题什么是". T是泛型类型.

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

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

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

这篇关于我的DAL应该返回DataTables还是我...不管什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆