开发者

LINQ to DataSet Dataclass assignment question

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 creat开发者_JAVA百科ed 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.


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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜