How to troubleshoot Cross-domain access after the Entity Framework queries anonymous objects

Source: Internet
Author: User
Tags anonymous tostring

In the Entity Framework, you can use lambda expressions to query data, and you can map query results directly to objects or object lists, which greatly improves development speed and makes data-tier data easier to process and deliver. But a lot of times, we do not need the whole table to find out the field, if we directly to the entire data entity query, it greatly affected the performance, so we need to query the anonymous object or the way has been defined to query the database;

1. Data entity classes used by the instance:

Publicclass Category
{
Publicint Id {get; set;}
Publicstring Name {get; set;}
Publicstring Description {get; set;}
}

2. Query

Suppose we need to reality a category directory, we only need ID and name two fields, we can build a new Categoryview

Publicclass Categoryview
{
Publicint Id {get; set;}
Publicstring Name {get; set;}
}

Query, you can do this so that it directly generates a list of Categoryview:

var data1 = from C into context. Categoriees
Select New Categoryview ()
{
Id = C.id,
Name = C.name
};

In this way, the method return value can be set to the iqueryable<categoryview> type, which can be traversed directly at the calling method.

However, when there is no categoryview, we can only use anonymous objects to query:

var data = from C into context. Categoriees
Select New
{
CId = C.id,
CName = C.name
};

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

After such a query, the same can be directly through the CID and CNAME value, but only within the current method, if Cross-domain access, you cannot directly read the CID and CNAME values:

The implementation of the Categoryservice class is as follows:

Namespace ConsoleApplication1
{
Publicclass Categoryservice
{
Private Shopcontext context=new shopcontext ();

Public iqueryable<object> getallcategories ()
{
var data = from C into context. Categoriees
Select New
{
CId = C.id,
CName = C.name
};
return data;
}
}
}

Call Method:

Categoryservice service=new Categoryservice ();
iqueryable<object> data = service. Getallcategories ();

At this point, we cannot directly read the attribute value of object type, we can use reflection to get the property value without considering the performance:

Namespace ConsoleApplication1
{
Class Program
{
Staticvoid Main (string[] args)
{
Categoryservice service=new Categoryservice ();
iqueryable<object> data = service. Getallcategories ();
foreach (var item in data)
{
Type type = Item. GetType ();
PropertyInfo Idproperty = type. GetProperty ("CId");
String id = idproperty.getvalue (item, NULL). ToString ();
PropertyInfo NameProperty = type. GetProperty ("CName");
String name = Nameproperty.getvalue (item, NULL). ToString ();

Console.WriteLine ("Id:" +id+ "Name:" +name);
}
Console.readkey ();
}
}
}

Of course, such an operation, if you feel that the performance of the problem is not more than the need to build the Categoryview model class caused by more trouble, is a relatively simple and quick implementation, but my suggestion is to build a view model class like Categoryview, Can send this class object pass to the view direct display, he le!!

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.