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.
我正在尝试构建一个接收Linq表的方法,并且应该返回一个List <>的值,这些值将是DropDownList数据源。
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)...
我不知道该怎么做,查询表只获取传递的字段(ValueField,TextField)...
Tks!
5 个解决方案
#1
3
Project the result of your LINQ2SQL query into a System.Collections.Generic.KeyValuePair object like so:
将LINQ2SQL查询的结果投影到System.Collections.Generic.KeyValuePair对象中,如下所示:
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.
然后,您需要将DropDownList上的DataValueField和DataTextField属性分别设置为“Key”和“Value”。
#2
1
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.
希望这有帮助,我真的不认为你需要创建一个while方法。但是如果你想我会说它需要一个IDictionary对象,并从那里转换它。
#3
0
Table.Select( t => t.field1, t.field2 )
Table.Select(t => t.field1,t.field2)
Also check out Scott Gutherie's blog series here.
另请参阅Scott Gutherie的博客系列。
#4
0
Are you trying to do something like the following with your method
您是否尝试使用您的方法执行以下操作
GetDropDownItems("Gates", "LastName") ????
GetDropDownItems(“盖茨”,“姓氏”)????
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
如果是这样,作为SDK示例的一部分包含一个名为DynamicQuery的项目。使用此功能,您基本上可以创建所需查询的文本版本。你可以做点什么
"LastName == 'Gates'"
“LastName =='盖茨'”
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
但是,自己构建表达式树也同样容易。了解表达式树的外观的最佳方法是使用ExpressionTreeVisualizer VS调试器添加(请注意,这也是SDK CSharpSamples中包含的另一个示例)。它会是这样的
ParameterExpression parameter = Expression.Parameter(typeof(T), "x"); var expression = Expression.Equals(Expression.Property(parameter, "LastName"), Expression.Constant("Gates")
ParameterExpression参数= Expression.Parameter(typeof(T),“x”); var expression = Expression.Equals(Expression.Property(parameter,“LastName”),Expression.Constant(“Gates”)
#5
0
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:
如果“Key”和“Value”是表示您想要获取的属性的名称的字符串,并且它们仅在运行时才知道...这是您的代码:
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;
}
#1
3
Project the result of your LINQ2SQL query into a System.Collections.Generic.KeyValuePair object like so:
将LINQ2SQL查询的结果投影到System.Collections.Generic.KeyValuePair对象中,如下所示:
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.
然后,您需要将DropDownList上的DataValueField和DataTextField属性分别设置为“Key”和“Value”。
#2
1
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.
希望这有帮助,我真的不认为你需要创建一个while方法。但是如果你想我会说它需要一个IDictionary对象,并从那里转换它。
#3
0
Table.Select( t => t.field1, t.field2 )
Table.Select(t => t.field1,t.field2)
Also check out Scott Gutherie's blog series here.
另请参阅Scott Gutherie的博客系列。
#4
0
Are you trying to do something like the following with your method
您是否尝试使用您的方法执行以下操作
GetDropDownItems("Gates", "LastName") ????
GetDropDownItems(“盖茨”,“姓氏”)????
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
如果是这样,作为SDK示例的一部分包含一个名为DynamicQuery的项目。使用此功能,您基本上可以创建所需查询的文本版本。你可以做点什么
"LastName == 'Gates'"
“LastName =='盖茨'”
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
但是,自己构建表达式树也同样容易。了解表达式树的外观的最佳方法是使用ExpressionTreeVisualizer VS调试器添加(请注意,这也是SDK CSharpSamples中包含的另一个示例)。它会是这样的
ParameterExpression parameter = Expression.Parameter(typeof(T), "x"); var expression = Expression.Equals(Expression.Property(parameter, "LastName"), Expression.Constant("Gates")
ParameterExpression参数= Expression.Parameter(typeof(T),“x”); var expression = Expression.Equals(Expression.Property(parameter,“LastName”),Expression.Constant(“Gates”)
#5
0
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:
如果“Key”和“Value”是表示您想要获取的属性的名称的字符串,并且它们仅在运行时才知道...这是您的代码:
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;
}