2013-04-05 75 views
0

使用EF4有可能通過財產上的財產,即linq OrderBy物業上的物業

具有Name屬性的EntityA。實體B有幾個其他屬性和EntityA。 我想通過EntityA.Name

entityBList = _repo.Find<EntityB>() .OrderBy(x => x.EntityA.Name); 
+1

請發佈您的代碼 – 2013-04-05 10:52:30

+1

是的。如果你有問題,你需要詳細說明。 – 2013-04-05 11:03:21

+0

在我的代碼中添加,它返回一個項目列表,只是無序 – 2013-04-05 11:31:16

回答

0

下令EntityB列表爲什麼不這項工作?我知道這不是使用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,序號1
EntityB.Name:B,序號2
EntityB.Name:A,序號3.