简体   繁体   中英

WP7 & Linq-To-Sql: Retrieve distinct recordset of specified column

I'm converting an existing data project to Windows Phone 7. There's a method that accepts a string value and uses it as a column name to select a distinct list of values:

public static List<string> GetDistinctValues( string Field ) {
    string sql = "SELECT DISTINCT [" + Field + "] FROM [MyTable]";
    ...
}

Converting this to Linq-to-Sql, I know how to use Distinct() , but I don't know how to dynamically set which column to query. I've tried searching and haven't found much. There are maybe a dozen different columns that may be used.

Sorry, I misunderstood your question originally.

What you are trying to do can be accomplished using Dynamic LINQ .

public static List<string> GetDistinctValues( string Field ) 
{
    var query = db.MyTable.Select(Field).Distinct();
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM