จัดเรียงข้อมูลโดย LINQ บน VB.NET 2010 มีปัญหาดูข้างในเลยครับ
sortData1 และ sortData2 มันไม่จัดเรียงให้ ช่วยหน่อยครับ
Code (VB.NET)
Sub Main()
Dim makeData = Students.GetAllStudents()
Dim sortData1 = From s In makeData Order By s.FirstName Ascending Order By s.LastName Descending
Select s
Dim sortData2 = From s In makeData Order By ("FirstName asc, LastName desc")
Select s
End Sub
Public Class Students
Public Property FirstName As String
Public Property LastName As String
Public Property Level As String 'A, B, C
Public Shared Iterator Function GetAllStudents() As IEnumerable(Of Students)
For i As Integer = 0 To 34 'จำนวนนักเรียนต่อห้อง = 35 คน
Yield New Students() With {.FirstName = Guid.NewGuid().ToString().Substring(0, 5),
.LastName = Guid.NewGuid().ToString().Substring(3, 5),
.Level = Chr(New Random().Next(65, 68))
}
Next
End Function
End Class
Tag : .NET, VB.NET, VS 2010 (.NET 4.x)
Date :
2014-05-16 02:12:39
By :
มึนไปหมดแล้ว
View :
1151
Reply :
3
Code (VB.NET)
MyList.OrderBy(x=> x.StartDate).ThenByDescending(x=> x.EndDate)
ตัวอย่างครับ
Date :
2014-05-16 09:07:24
By :
mr.win
ขอบคุณครับ
หายมึนและรับรู้สาเหตุของมันแล้วครับ
http://stackoverflow.com/questions/722868/sorting-a-list-using-lambda-linq-to-objects
This time for any Enumerable:
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
public static class EnumerableHelper
{
static MethodInfo orderBy = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(x => x.Name == "OrderBy" && x.GetParameters().Length == 2).First();
public static IEnumerable<TSource> OrderBy<TSource>(this IEnumerable<TSource> source, string propertyName)
{
var pi = typeof(TSource).GetProperty(propertyName, BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance);
var selectorParam = Expression.Parameter(typeof(TSource), "keySelector");
var sourceParam = Expression.Parameter(typeof(IEnumerable<TSource>), "source");
return
Expression.Lambda<Func<IEnumerable<TSource>, IOrderedEnumerable<TSource>>>
(
Expression.Call
(
orderBy.MakeGenericMethod(typeof(TSource), pi.PropertyType),
sourceParam,
Expression.Lambda
(
typeof(Func<,>).MakeGenericType(typeof(TSource), pi.PropertyType),
Expression.Property(selectorParam, pi),
selectorParam
)
),
sourceParam
)
.Compile()(source);
}
public static IEnumerable<TSource> OrderBy<TSource>(this IEnumerable<TSource> source, string propertyName, bool ascending)
{
return ascending ? source.OrderBy(propertyName) : source.OrderBy(propertyName).Reverse();
}
}
Another one, this time for any IQueryable:
Code (C#)
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class IQueryableHelper
{
static MethodInfo orderBy = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(x => x.Name == "OrderBy" && x.GetParameters().Length == 2).First();
static MethodInfo orderByDescending = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(x => x.Name == "OrderByDescending" && x.GetParameters().Length == 2).First();
public static IQueryable<TSource> OrderBy<TSource>(this IQueryable<TSource> source, params string[] sortDescriptors)
{
return sortDescriptors.Length > 0 ? source.OrderBy(sortDescriptors, 0) : source;
}
static IQueryable<TSource> OrderBy<TSource>(this IQueryable<TSource> source, string[] sortDescriptors, int index)
{
if (index < sortDescriptors.Length - 1) source = source.OrderBy(sortDescriptors, index + 1);
string[] splitted = sortDescriptors[index].Split(' ');
var pi = typeof(TSource).GetProperty(splitted[0], BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.IgnoreCase);
var selectorParam = Expression.Parameter(typeof(TSource), "keySelector");
return source.Provider.CreateQuery<TSource>(Expression.Call((splitted.Length > 1 && string.Compare(splitted[1], "desc", StringComparison.Ordinal) == 0 ? orderByDescending : orderBy).MakeGenericMethod(typeof(TSource), pi.PropertyType), source.Expression, Expression.Lambda(typeof(Func<,>).MakeGenericType(typeof(TSource), pi.PropertyType), Expression.Property(selectorParam, pi), selectorParam)));
}
}
Date :
2014-05-16 10:14:13
By :
มึนไปหมดแล้ว
Date :
2014-05-16 10:21:19
By :
mr.win
Load balance : Server 04