Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Considerations for LINQ to Entities query

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1 > sorting information is lost

If any other actions are performed after the sort operation, there is no guarantee that the sort results will be retained in these additional operations. These operations include Select and Where. In addition, the First and FirstOrDefault methods that take expressions as input parameters do not retain order.

The following code: can not achieve the effect of reverse sorting

Using (var edm = new NorthwindEntities ()) {IQueryable cc = edm.Customers.OrderByDescending (c = > c.CustomerID) .Where (c = > c.Region! = null) .Select (c = > c); foreach (var c in cc) Console.WriteLine (c.CustomerID);}

2 > unsigned integers are not supported

Because the entity framework does not support unsigned integers, specifying unsigned integer types in LINQ to Entities queries is not supported. If you specify an unsigned integer, a NotSupportedException exception is thrown during query expression conversion, and a constant value of type end cannot be created. This context supports only primitive types ("for example, Int32, String, and Guid").

The following code will report the exception:

Using (var edm = new NorthwindEntities ()) {uint id = UInt32.Parse ("123"); IQueryable produt = from p in edm.Products where p.UnitPrice = = id select p.ProductName; foreach (string name in produt) Console.WriteLine (name);}

In the above code, because id is a scalar type of uint rather than Int32,String,Guid, an exception is reported when executed to the place where p.UnitPrice = = id.

3 > reference to non-scalar closures is not supported

References to non-scalar closures (such as entities) in queries are not supported. When such a query is executed, a NotSupportedException exception is thrown and the message "cannot create a constant value of type end". Only primitive types ('such as Int32, String, and Guid') are supported in this context

The following code will report the exception:

Using (var edm = new NorthwindEntities ()) {Customers customer = edm.Customers.FirstOrDefault (); IQueryable cc = from c in edm.Customers where c = = customer select c.ContactName; foreach (string name in cc) Console.WriteLine (name);}

In the above code, because customer is a reference type rather than a scalar type of Int32,String,Guid, an exception is reported when executed to the where c==customer place.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report