tags:

views:

625

answers:

5

I'm trying to build a method that will receive a Linq table, and should return a List<> of values that will be a DropDownList Datasource.

This is what I've got till now:

public static List<Structs.NameValuePair> GenDropDownItens<T>(string ValueField , string TextField ) where T: class

What i don't know how to do is, query the table getting only the fields that are passed ( ValueField, TextField)...

Tks!

A: 

Table.Select( t => t.field1, t.field2 )

Also check out Scott Gutherie's blog series here.

Daniel M
Table.Select(t => new Structs.NameValuePair(t.field1, t.field2)).ToList()- Even better.
Daniel M
Dan, did you read my answer? ;)
CodeChef
Oops, No... Have now though - it's a good one... +1!
Daniel M
+1  A: 

Why not just do something like;

var dropDownValues = dataContext.SomeTable.ToDictionary(
    s => s.Name,
    s => s.Value
);

foreach(var item in dropDownValues) {
    var OptionName = item.Key;
    var OptionValue = item.Value
};

Hope this helps, I really don't think you need to create a while method. But if you wanted to I would say have it take a IDictionary object, and convert it from there.

Nick Berardi
I think he needs to be able to supply the "Key, Value" field names as strings at runtime.
Timothy Khouri
A: 

Are you trying to do something like the following with your method

GetDropDownItems("Gates", "LastName") ????

If so, included as part of the SDK samples is a project called DynamicQuery. Using this you can basically create a text version of the query you want. You could do something like

"LastName == 'Gates'"

However, it is just as easy to build the expression tree yourself. The best way to learn what the expressions tree's look like is to use the ExpressionTreeVisualizer VS debugger add in (note this is also another sample contained in the SDK CSharpSamples). It would be something like

ParameterExpression parameter = Expression.Parameter(typeof(T), "x"); var expression = Expression.Equals(Expression.Property(parameter, "LastName"), Expression.Constant("Gates")

+3  A: 

Project the result of your LINQ2SQL query into a System.Collections.Generic.KeyValuePair object like so:


ddl.DataSource = DataContext.Table.Select(o => new KeyValuePair<string, string>(o.ID, o.DisplayField));
ddl.DataBind();

You will then want to set the DataValueField and DataTextField attributes on the DropDownList to "Key" and "Value" respectively.

CodeChef
CORRECTION: The lambda in the select should be: o => new KeyValuePair<string, string>(o.ID, o.DisplayField)
CodeChef
Sometimes it's fun to think about these things. You might be able to project your query directly into a System.Web.UI.WebControls.ListItem object. This might allow you to avoid having to set the attributes on the DropDownList. I haven't tried this though - let me know if you try it and it works.
CodeChef
@CodeChef: added the lt/gt HTML entities to make your answer show up properly
sixlettervariables
Thanks. I didn't an edit button after posting.
CodeChef
A: 

If "Key" and "Value" are strings that represent the name of the properties you would like to get, and they are known only at runtime... here's your code:

private static Func<T, DictionaryEntry> GetNameValuePairFunc<T>(string valueField, string textField)
{
    Func<T, DictionaryEntry> result = (item) =>
    {
     object key = typeof(T).GetProperty(valueField).GetValue(item, null);

     object text = typeof(T).GetProperty(textField).GetValue(item, null);

     return new DictionaryEntry(key, text);
    };

    return result;
}
Timothy Khouri
If the issue is simply changing the the fields that are bound to the DropDownList Key and Text, why wouldn't he simply bind is LINQ object and use the DataValueField and DataTextField attributes on the DropDownList?
CodeChef
That's an excellent point... and that's what *I* would do. But I'm guessing / hoping that there is more to this that we don't know :)
Timothy Khouri