The stochastic scheduling architecture for ~linq to entity EF

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Back to the directory

For from LINQ to SQL migrated to developers, in random order will not feel strange, directly to the DataContext a method to add with the reflection can be achieved in random order, the code as follows:

    /// <summary>
    /// Data context extension
    /// </summary>
    public partial class dbDataContext : IUnitOfWork
    {
        /// <summary>
        /// Use this function to random sort
        /// </summary>
        /// <returns></returns>
        [Function(Name = "NewID", IsComposable = true)]
        public Guid NewID()
        {
            return ((Guid)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));

        }
     }

But for LINQ to entity developers will not use the above methods., Because the dbcontext does not have the ExecuteMethodCall method, Ha-ha, Only you want to track., Actually, For the characteristics of a EdmFunction in system.Data.Entity space, It with the LINQ to Function SQL in the similar characteristics, Are identified as function, Is a function of the use of the data source, Such as the newid function Sqlserver, We can arrange it thought, The code will come out, We like it a domain independent code in the core project., Expressed as a common code

    /// <summary>
    /// Extension of the class of SQL functions
    /// </summary>
    public static class SqlFunctionExtensions
    {
        /// <summary>
        /// In the LINQ to using the SqlServer.NEWID function in entity
        /// </summary>
        [System.Data.Objects.DataClasses.EdmFunction("SqlServer", "NEWID")]
        public static Guid NewId()
        {
            return Guid.NewGuid();
        }
    }

In order to make developers method in use, we take NewId as a method of extending IEnumerable interface, so whether it is IQueryable or IList, the List collection can use it directly in the code, see

    /// <summary>
    /// Extension of the class of SQL functions
    /// </summary>
    public static class SqlFunctionExtensions
    {
        #region Function method
        /// <summary>
        /// In the LINQ to using the SqlServer.NEWID function in entity
        /// </summary>
        [System.Data.Objects.DataClasses.EdmFunction("SqlServer", "NEWID")]
        public static Guid NewId()
        {
            return Guid.NewGuid();
        }
        #endregion

        #region Extension methods
        /// <summary>
        /// Random sort extension method
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static IQueryable<T> OrderByNewId<T>(this IEnumerable<T> source)
        {
            return source.AsQueryable().OrderBy(d => NewId());
        }
        #endregion

    }

And the use of a collection of IEnumerable extensions, any delay load characteristics, through our testing, the answer is yes, it is time, here is our example

      public ActionResult Index()
        {
            var list = irepository.GetEntities();
            ViewBag.List = list.OrderByNewId().Take(5).ToList();//Only 5
            return View();
        }

The results are as follows:

The database results, is to our satisfaction, only took 5 data

 OK, To this use of the LINQ to entity (entity frameworks environment) random sequence is introduced to here, thank you for readingļ¼

Back to the directory

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Charles at November 17, 2013 - 10:31 AM