日期:2014-05-20  浏览次数:20742 次

linq排序的问题,求救……
C# code

       class boolcompare : IComparer<bool ?>
        {
            public int Compare(bool? b1, bool? b2)
            {
                if (b1 != null && b2 != null)
                {
                    return b1.Value.CompareTo(b2.Value);
                }
                else if (b1 == null && b2== null)
                {
                    return 0;
                }
                else if (b1 == null && b2 == true)
                {
                    return 1;
                }
                else if (b1 == null && b2 == false)
                {
                    return -1;
                }
                else if (b1 == true && b2 == null)
                {
                    return -1;
                }
                else if (b1 == false && b2 == null)
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }

            public static IComparer<bool ?> boolcompareAscending()
            {
                return (IComparer<bool ?>)(new boolcompare());
            }
        }

     public List<Information> getinforMationByReceverID(int receverID)
        {
            //临时的比较器类
            IComparer<bool?> bx = boolcompare.boolcompareAscending();
            return em.Information.Where(f => f.ReceiveUserID == receverID).OrderBy(g => (g.IfRead)).ThenBy(g => g.IfDo,bx).ThenByDescending(g => (g.SendDate)).ToList();
        }


.ThenBy(g => g.IfDo,bx)这会报错
LINQ to Entities 不识别方法“……”,因此该方法无法转换为存储表达式。
求大侠指教如何排序
或者给个别的方法 IfDo 这个字段需要按 false,null,true这样的顺序排序


------解决方案--------------------
既然 linq2EF不能识别表达式
那就先转化成linq2object试试 :
return em.Information.Where(f => f.ReceiveUserID == receverID).ToList().OrderBy(g => (g.IfRead)).ThenBy(g => g.IfDo,bx).ThenByDescending(g => (g.SendDate)).ToList();