Using the Entity Framework and WCF Ria services to develop Silverlight 6: Find specified fields

Source: Internet
Author: User
Tags silverlight

The search for a specified field in a database table is a necessary work in the actual work. The SL client obtains only the specified fields that are actually needed, for example: helps reduce network traffic.

There are two types of such usage scenarios.

1: The table query does not require a foreign key table

In the previous article, we used a table query, which is the code for the Dal section in a table query:

We have pointed out that whether you use the Include method or not, as long as the entity class specifies the include attribute, the client obtains the associated course record. Admittedly, there are times when we need to meet a table query, but we don't need it at other times.

2: Only partial fields are required in a single table

For example: A table has 5 fields, and I only need to return two fields.

3: implementation

To meet this requirement, only the DAL section needs to be modified.

First requirement, remove the specified field:

The SQL statement generated by this code is as follows:

SELECT[Extent1].[DepartmentID] AS[DepartmentID], [Extent1].[Name] AS [Name]FROM [dbo].[Department] AS[Extent1]

As you can see, EF only queries two fields for us. Also, EF does not automatically correlate data for course tables for us.

Note that in this code, we first select New with an anonymous type, bypassing the dbset<t> in DbContext for department type validation. If we take this code for granted:

Then, we will find that we cannot get the data, trace the error, and we will find:

{"The Entity or complex type ' schooldata.department ' cannot is constructed in a LINQ to Entities query."} system.systemexception {System.NotSupportedException}
The results of the final run are as follows:

Looking back, what do you do if you want to specify a field and query the table again? As follows:

The final results are as follows:

Since the code in this section was modified in the previous article, it is no longer available for download.

PS: The whole circle back, only to find that Dudu has studied this problem very deep, in:

Http://www.cnblogs.com/dudu/archive/2011/04/01/entity_framework_select_new_ok.html

Http://www.cnblogs.com/dudu/archive/2011/03/31/entity_framework_select_new.html

Using the Entity Framework and WCF Ria services to develop Silverlight 6: Find specified fields

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.