English 中文(简体)
LINQ to SQL Dynamic Sort question
原标题:

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ:

select 
     type_id as id,
     type_desc as description 
from
     dbo.equip_type_avt
order by
     description

What do I replace the ????? in my .Select clause in my LINQ statement?

   public IQueryable<equip_type_avt> GetGridEquipmentTypes(string sidx, string sord)
    {

        try
        {
            return
                ulsDB.equip_type_avts
                    .Select(?????)
                    .OrderBy(sidx + " " + sord);


        }
        catch (Exception ex)
        {
            string strErr = ex.Message;
            return null;
        }

    }
最佳回答

You can use an anonymous type:

table.Select(x => new 
             {
               ID = x.type_id,
               Description = x.type_desc
             });

However, you can t access the properties of an anonymous type outside of the scope where it is declared (without reflection or other dirty hackery, anyway) so if you want to use the result outside of that function you just create a class and create an instance of it in the query using a type initializer:

public class Foobar
{
  public int ID { get; set; }
  public string Description { get; set; }
}

...

table.Select(x => new Foobar() // Note the difference here
             {
               ID = x.type_id,
               Description = x.type_desc
             });

Question though: if you want to name the columns differently, why don t you change it in the place where the column-property mapping is declared? In LINQ-to-SQL you can have the database column be named whatever you like but give the property the name "ID" or "Description".

问题回答

I m not sure i understand your question, how does sidx and sord relate to your query?

Isn t your problem rather that you have to end your query with OrderBy(...).ThenBy(...) instead of a combined OrderBy?

If you want to sort by a string in the easy way, download the Dynamic LINQ library.

However, that s 2000 lines of code, most of which are entirely redundant for just the purpose of sorting.

Doing it yourself shouldn t be too hard, but requires a fair bit of knowledge on expression trees. I can t really help you there though.

EDIT: I ve added another answer, that hopefully answers your actual question :)





相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签