4

我正在使用EF + RIA,不幸遇到了與相關實體排序有關的一些問題。 爲了這樣的目的有ESQL查詢我實現了(只發現了這個解決方案):將ESQL轉換爲LINQ to Entities。按相關實體排序

var queryESQL = string.Format(
@" select VALUE ent from SomeEntities as ent 
    join Attributes as ea ON ea.EntityId = ent.Id 
    where ea.AttributeTypeId = @typeId 
    order by ea.{0} {1}", columnName, descending ? "desc" : "asc"); 

var query = ObjectContext.CreateQuery<SomeEntity>(queryESQL, new ObjectParameter("typeId", attributeTypeId));               

表具有以下結構:

<Attribute>: 
    int Id; 
    decimal DecimalColumn; 
    string StringColumn; 
    int EntityId; 
    int AttributeTypeId; 

<SomeEntity>: 
    int Id; 
    string Name; 

有什麼辦法來改寫這個東西(排序),使用LINQ to Entities的方法?

回答

2

這是我的嘗試,我不能保證它會工作。 我需要更多地考慮如何獲得動態列名稱,但我不確定。編輯:您可以使用字符串作爲訂單欄。

int typeId = 1115; 
bool orderAscending = false; 
string columnName = "StringColumn"; 
var query = from ent in SomeEntities 
join ea in Attributes on ea.EntityId = ent.Id 
where ea.AttributeTypeId = typeId; 

if(orderAscending) 
{ 
    query = query.OrderBy(ea => columnName).Select(ea => ea.Value); 
} 
else 
{ 
    query = query.OrderByDescending(ea => columnName).Select(ea => ea.Value); 
} 

var results = query.ToList(); //調用toList或枚舉執行查詢,因爲LINQ延遲執行。

編輯:我認爲,選擇停止後排序是從排序。我將select語句移到了order by之後。我還添加了「query =」,但我不確定是否需要。目前我沒有辦法測試它。

編輯3:我今天解僱了LINQPad,並對之前的東西做了一些調整。我使用基於代碼的方法將數據建模爲使用EF,並且應該接近您的數據。 如果你只是想獲得一個屬性列表(你不是),這種方法效果會更好。爲了解決這個問題,我添加了一個Entity屬性給MyAttribute類。 此代碼在LINQPAD中可用。

void Main() 
{ 
    // add test entities as needed. I'm assuming you have an Attibutes collection on your Entity based on your tables. 
    List<MyEntity> SomeEntities = new List<MyEntity>(); 
    MyEntity e1 = new MyEntity(); 
    MyAttribute a1 = new MyAttribute(){ StringColumn="One", DecimalColumn=25.6M, Id=1, EntityId=1, AttributeTypeId = 1, Entity=e1 }; 
    e1.Attributes.Add(a1); 
    e1.Id = 1; 
    e1.Name= "E1"; 
    SomeEntities.Add(e1); 

    MyEntity e2 = new MyEntity(); 
    MyAttribute a2 = new MyAttribute(){ StringColumn="Two", DecimalColumn=198.7M, Id=2, EntityId=2, AttributeTypeId = 1, Entity=e2 }; 
    e2.Attributes.Add(a2); 
    e2.Id = 2; 
    e2.Name = "E2"; 
    SomeEntities.Add(e2); 

    MyEntity e3 = new MyEntity(); 
    MyAttribute a3 = new MyAttribute(){ StringColumn="Three", DecimalColumn=65.9M, Id=3, EntityId=3, AttributeTypeId = 1, Entity=e3 }; 
    e3.Attributes.Add(a3); 
    e3.Id = 3; 
    e3.Name = "E3"; 
    SomeEntities.Add(e3); 

    List<MyAttribute> attributes = new List<MyAttribute>(); 
    attributes.Add(a1); 
    attributes.Add(a2); 
    attributes.Add(a3); 

    int typeId = 1; 
    bool orderAscending = true; 
    string columnName = "StringColumn"; 
    var query = (from ent in SomeEntities 
    where ent.Attributes.Any(a => a.AttributeTypeId == typeId) 
    select ent.Attributes).SelectMany(a => a).AsQueryable(); 
    query.Dump("Pre Ordering"); 
    if(orderAscending) 
    { 
     // query = is needed 
     query = query.OrderBy(att => MyEntity.GetPropertyValue(att, columnName)); 
    } 
    else 
    { 
     query = query.OrderByDescending(att => MyEntity.GetPropertyValue(att, columnName)); 
    } 

    // returns a list of MyAttributes. If you need to get a list of attributes, add a MyEntity property to the MyAttribute class and populate it 
    var results = query.Select(att => att.Entity).ToList().Dump(); 
} 

// Define other methods and classes here 
} 
    class MyAttribute 
    { 
    public int Id { get; set; } 
    public decimal DecimalColumn { get; set; } 
    public string StringColumn { get; set; } 
    public int EntityId { get; set; } 
    public int AttributeTypeId { get; set; } 
    // having this property will require an Include in EF to return it then query, which is less effecient than the original ObjectQuery< for the question 
    public MyEntity Entity { get; set; } 

    } 
    class MyEntity 
    { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public ICollection<MyAttribute> Attributes { get; set; } 
    public MyEntity() 
    { 
    this.Attributes = new List<MyAttribute>(); 
    } 

    // this could have been on any class, I stuck it here for ease of use in LINQPad 
    // caution reflection may be slow 
    public static object GetPropertyValue(object obj, string property) 
{ 
// from Kjetil Watnedal on http://stackoverflow.com/questions/41244/dynamic-linq-orderby 
    System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property); 
    return propertyInfo.GetValue(obj, null); 
} 
+0

我不是很舒服,但'OrderBy'上下文中不會有'Column1'屬性。 'ea'表示有一個'SomeEntity'。 –

+0

我在完成我的答案時不得不離開。 Column1當時只是一個佔位符。我已經更新了我的答案,以顯示如何動態地對查詢進行排序。 – Aligned

+0

這裏的主要問題是按「屬性」進行排序。按名稱排序不是這裏的目標。 –