How to read data randomly by Linq
This article mainly introduces how Linq reads data randomly, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Linq randomly reads data
Add this method to the system's freely generated obind p mapping code, and the same is true if it is user-written (or tool-generated) obind p mapping code. Here I'm going to talk about my own. The LINQ To Sql class generated by the system produces three files. Northwind.cs, Northwind.dbml.layout, Northwind.designer.cs
All we have to do is add the method NEWID () we need in Northwind.designer.cs, which is, of course, consistent with the function of NEWID () in the database.
The specific method code is as follows:
[System.Data.Linq.Mapping.DatabaseAttribute (Name= "Northwind")]
Public partial class NorthwindDataContext: System.Data.Linq.DataContext
{
Private static System.Data.Linq.Mapping.
MappingSource mappingSource = new AttributeMappingSource ()
/ / add to the automatically generated mapping code
[Function (Name = "NEWID", IsComposable = true)]
Public Guid NEWID ()
{
Return ((Guid) (this.ExecuteMethodCall (this)
((MethodInfo) (MethodInfo.GetCurrentMethod () .ReturnValue))
}
/ / the following generated code is slightly..
By regenerating and writing this, our access implementation becomes easy, and it is used in the same way as traditional access principles.
Db = new NorthwindDataContext (); var result = (from c in db.Customers orderby db.NEWID () select c) .Take (10); foreach (var item in result) Console.WriteLine (item.CompanyName); Console.ReadLine () Thank you for reading this article carefully. I hope the article "how to read data randomly from Linq" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!