使用EF4可以通过属性上的属性进行排序,即
具有Name属性的EntityA。 实体B还有一些其他属性和EntityA。 我想获得EntityA.Name
订购的EntityB列表entityBList = _repo.Find<EntityB>() .OrderBy(x => x.EntityA.Name);
答案 0 :(得分:0)
为什么这不起作用?我知道它不是使用EF4,但概念是一样的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqOrderByReference
{
class Program
{
static void Main(string[] args)
{
EntityA[] entitiesA = { new EntityA { Ordinal = 3, SecretCode = "xxx"},
new EntityA { Ordinal = 2 ,SecretCode = "x"},
new EntityA { Ordinal = 1 ,SecretCode = "x"} };
EntityB[] entitiesB = { new EntityB { Name = "C", EntityA = entitiesA[2] },
new EntityB {Name = "B", EntityA = entitiesA[1] },
new EntityB {Name = "A", EntityA = entitiesA[0] } };
IEnumerable<EntityB> entitiesBList = entitiesB.OrderBy(x => x.EntityA.Ordinal);
foreach (EntityB b in entitiesBList)
{
Console.WriteLine("EntityB.Name: {0}, Ordinal {1}.", b.Name, b.EntityA.Ordinal);
}
Console.Read();
}
}
public class EntityA
{
public int Ordinal { get; set; }
public string SecretCode { get; set; }
}
public class EntityB
{
public string Name { get; set; }
public EntityA EntityA { get; set; }
}
}
输出是:
EntityB.Name:C,Ordinal 1.
EntityB.Name:B,Ordinal 2.
EntityB.Name:A,Ordinal 3.
答案 1 :(得分:0)
name属性是一个简单的字符串,还是包含First name和Last Name,如果是,它会覆盖GetHashCode方法?如果是这样,您可能需要查看此帖子What is the best algorithm for an overridden System.Object.GetHashCode?