Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Considerations for LINQ to Entities query

Shulou Source: shulou.com Published: 2022-06-01 07:59:41 10月01日 Update

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.

Tags: Type support code query integer symbol report sort up and down context place local time constant scalar expression closure facet non-standard middle finger information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi MariaDB NVidia Linux