LINQ to DataSet 数据类分配问题

发布于 2024-08-20 09:28:30 字数 1107 浏览 3 评论 0原文

我正在开发一个 Silverlight 项目,尝试使用 LINQ To DataSet 访问数据库,然后通过 .ASMX Web 服务将数据发送到 Silverlight。

我已经使用服务器资源管理器工具定义了我的数据集(拖放我感兴趣的所有不同表)。数据集能够毫无问题地访问服务器和数据库。

下面是来自我的 Web 方法之一的代码:

    public  List<ClassSpecification> getSpecifications()
    {
        DataSet2TableAdapters.SpecificationTableAdapter Sta = new DataSet2TableAdapters.SpecificationTableAdapter();

        return (from Spec in Sta.GetData().AsEnumerable()
                select new ClassSpecification()
                {
                    Specification = Spec.Field<String>("Specification"),
                    SpecificationType = Spec.Field<string>("SpecificationType"),
                    StatusChange = Spec.Field<DateTime>("StatusChange"),
                    Spec = Spec.Field<int>("Spec")
                }).ToList<ClassSpecification>();
    }

我创建了一个“ClassSpecification”数据类,它将包含我的数据,并且它具有所有表字段作为属性。

我的问题是,是否有比此处显示的更快的方法来完成作业?实际上还有大约 10 个字段,我想,由于我的 DataSet 知道我的表定义,因此我将有一种比逐个字段更快的方法来完成分配。我尝试只是“选择新的 ClassSpecification()).ToList

任何帮助将不胜感激。

I'm working on a Silverlight project trying to access a database using LINQ To DataSet and then sending data over to Silverlight via .ASMX web service.

I've defined my DataSet using the Server Explorer tool (dragging and dropping all the different tables that I'm interested in). The DataSet is able to access the server and database with no issues.

Below is code from one of my Web Methods:

    public  List<ClassSpecification> getSpecifications()
    {
        DataSet2TableAdapters.SpecificationTableAdapter Sta = new DataSet2TableAdapters.SpecificationTableAdapter();

        return (from Spec in Sta.GetData().AsEnumerable()
                select new ClassSpecification()
                {
                    Specification = Spec.Field<String>("Specification"),
                    SpecificationType = Spec.Field<string>("SpecificationType"),
                    StatusChange = Spec.Field<DateTime>("StatusChange"),
                    Spec = Spec.Field<int>("Spec")
                }).ToList<ClassSpecification>();
    }

I created a "ClassSpecification" data class which is going to contain my data and it has all the table fields as properties.

My question is, is there a quicker way of doing the assignment than what is shown here? There are actually about 10 more fields, and I would imagine that since my DataSet knows my table definition, that I would have a quicker way of doing the assignment than going field by field. I tried just "select new ClassSpecification()).ToList

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

单身狗的梦 2024-08-27 09:28:30

首先,我建议使用 LINQPad 测试不同的可能性,它是免费的并且非常棒。

我不太记得你可以从表适配器中做什么,但是你应该能够使用 DataSet 来获取你想要的数据,例如

string spec = myDataSet.MyTable.Rows[0] // or FindBy... or however you are choosing a row
    .Specification;

所以你可能能够做

foreach(var row in myDataSet.MyTable.Rows) {
    string spec = row.Specification;
    ...
}

或者

return (from row in myDataSet.Specification
    select new ClassSpecification()
    {
        Specification = row.Specification,
        SpecificationType = row.SpecificationType,
        StatusChange = row.StatusChange,
        Spec = row.Spec,
    }).ToList<ClassSpecification>();

或者 甚至

return myDataSet.Specification.Cast<ClassSpecification>()

不确定最后一个是否会工作,但您可以看到有多种方法可以获得您想要的东西。另外,在我的测试中,行是强类型的,因此您不需要创建一个新类来放置数据 - 您应该能够使用现有的“SpecificationRow”类。 (事实上​​,我相信这是贫血域模型反模式。)

First, I'll recommend testing out different possibilities using LINQPad, which is free and awesome.

I can't quite remember what you can do from the table adapter, but you should be able to use the DataSet to get at the data you want, e.g.

string spec = myDataSet.MyTable.Rows[0] // or FindBy... or however you are choosing a row
    .Specification;

So you might be able to do

foreach(var row in myDataSet.MyTable.Rows) {
    string spec = row.Specification;
    ...
}

Or

return (from row in myDataSet.Specification
    select new ClassSpecification()
    {
        Specification = row.Specification,
        SpecificationType = row.SpecificationType,
        StatusChange = row.StatusChange,
        Spec = row.Spec,
    }).ToList<ClassSpecification>();

Or even

return myDataSet.Specification.Cast<ClassSpecification>()

Not sure if the last one will work, but you can see that there are several ways to get what you want. Also, in my tests the row is strongly typed, so you shouldn't need to create a new class in which to put the data - you should just be able to use the existing "SpecificationRow" class. (In fact, I believe that this is the anemic domain model anti-pattern.)

又怨 2024-08-27 09:28:30

那么您是否因为缺少数据库的 Linq 提供程序而使用数据集?这是我考虑此举的唯一原因。例如,使用 Linq to Sql,您可以将表拖出并放下。然后你就得到了具有你想要的形状的即时对象。

So are you using a dataset for lack of a Linq provider to the database? That is the only reason I would consider this move. For instance, with Linq to Sql you can drag the table out and drop it. Then you have instant objects with the shape you want for it.

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