LINQ projection queries and alternatives in data point-WCF services

Source: Internet
Author: User
Tags anonymous

When my local. NET User group presenter was writing a LINQ query in class last month, I asked him, "How did we get past LINQ before?" He replied, "It's hard to imagine."

It's true. Since LINQ was introduced to Visual Studio in 2008, it has had such a significant impact on how we programmatically program in the Microsoft. NET Framework. Combined with many of the new language features introduced in Visual Basic and C #, LINQ solves the problem of querying objects and data sources in memory consistently.

LINQ has the ability to project random shapes of data into anonymous types, which has both a good and a bad side. Anonymous types are a good solution if you only need to get a specific view of the data without having to declare a new class for the one-time type. The projection and anonymous types of LINQ really spoil us. So why do I say that they also have a bad side?

You might be aware of this if you used a LINQ projection for a method that needs to return data to another method, or worse, to use LINQ projections in Windows Communication Foundation (WCF) service operations.

The reason is that anonymous types are one-off types, they are not declared, and only the way they are created can understand them. If you write a query that returns a list of anonymous types, because there is no way to express "anonymous type", there is no way to define a method parameter, saying: "I will return a column ...".

The following is a LINQ to entities query with a simple projection:

var custQuery = from c in context.Customers

          select new {c.CustomerID,  Name=c.LastName.Trim() +

          ", " + c.FirstName};

At run time, the Custquery variable will actually become a objectquery<<>f__anonymoustype0<int,string>>.

With Var (and the alternative use of Visual Basic Dim) We no longer need to find this untyped expression.

If you want to return the results of the query from a method, the only reasonable solution is to create a class that represents the type to be returned. However, it makes no sense to provide an anonymous type. Now you have to write more code, define classes, and you might want to define new projects that hold new classes, and make sure that the assemblies that use those classes have access to them, and so on.

Recently, data services have given another challenge. To project the data, you must create a custom action in the service, execute your own query, and then return a predefined class that can be understood by the client.

When you are working on a service, in many cases you want to work with a particular view of the data without having to move a large type over the line.

To meet this temporary requirement, you have more options than creating additional types in your domain.

New projection features in WCF Data Services

The. NET Framework 3.5 SP1 Data Service update introduces a few powerful features to WCF data Services, which is part of the. NET Framework 4. These features include the ability to use projections for data services in queries. It is highly recommended that you view the blog posts of the WCF Data Services team (blogs.msdn.com/astoriateam/archive/2010/01/27/data-services-update- for-net-3-5-sp1-available-for-download.aspx) to understand all the new features of this update.

$select operator was added to the data service URI syntax. It allows the use of properties and even the navigation property projection.

The following is a brief example of a projection of several scalar properties of a customer with the SalesOrderHeaders navigation property:

http://localhost /DataService.svc/Customers(609)
  $select=CustomerID,LastName,FirstName,SalesOrderHeaders&$expand=
  SalesOrderHeaders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.