我有一个linq查询,它选择占位符中的所有文本框,并使用结构将它们添加到列表中.我需要扩展此功能以获取DropDownList的selectedvalue我很确定我做错了,因为当我调试方法时,列表计数为0.
我自己的猜测是声明2 OfType<>()
是错误的,但我对linq很新,我不知道怎么做.
任何帮助都是极好的!提前致谢.
这是我到目前为止所拥有的:
public struct content
{
public string name;
public string memberNo;
public int points;
public string carclass;
}
List<content> rows = new List<content>();
protected void LinkButton_Submit_Attendees_Click(object sender, EventArgs e)
{
List<content> rows = PlaceHolder_ForEntries.Controls.OfType<TextBox>().OfType<DropDownList>()
.Select(txt => new
{
Txt = txt,
Number = new String(txt.ID.SkipWhile(c => !Char.IsDigit(c)).ToArray())
})
.GroupBy(x => x.Number)
.Select(g => new content
{
carclass = g.First(x => x.Txt.ID.StartsWith("DropDownlist_CarClass")).Txt.SelectedValue,
name = g.First(x => x.Txt.ID.StartsWith("TextBox_Name")).Txt.Text,
memberNo = g.First(x => x.Txt.ID.StartsWith("TextBox_MemberNo")).Txt.Text,
points = int.Parse(g.First(x => x.Txt.ID.StartsWith("TextBox_Points")).Txt.Text)
})
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
这是创建控件的方法.
protected void createcontrols()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
while (PlaceHolder_ForEntries.Controls.Count < count)
{
TextBox TextBox_Name = new TextBox();
TextBox TextBox_MemberNo = new TextBox();
TextBox TextBox_Points = new TextBox();
DropDownList DropDownList_CarClass = new DropDownList();
DropDownList_CarClass.Items.Add("Car1");
...
DropDownList_CarClass.Items.Add("Car2");
TextBox_Name.Attributes.Add("placeholder", "Navn");
TextBox_Name.ID = "TextBox_Name" + PlaceHolder_ForEntries.Controls.Count.ToString();
TextBox_Name.CssClass = "input-small";
TextBox_MemberNo.Attributes.Add("placeholder", "Medlemsnr.");
TextBox_MemberNo.ID = "TextBox_MemberNo" + PlaceHolder_ForEntries.Controls.Count.ToString();
TextBox_MemberNo.CssClass = "input-small";
TextBox_Points.Attributes.Add("placeholder", "Point");
TextBox_Points.ID = "TextBox_Points" + PlaceHolder_ForEntries.Controls.Count.ToString();
TextBox_Points.CssClass = "input-small";
PlaceHolder_ForEntries.Controls.Add(TextBox_Name);
PlaceHolder_ForEntries.Controls.Add(TextBox_MemberNo);
PlaceHolder_ForEntries.Controls.Add(DropDownList_CarClass);
PlaceHolder_ForEntries.Controls.Add(TextBox_Points);
PlaceHolder_ForEntries.Controls.Add(new LiteralControl("<br />"));
}
}
Run Code Online (Sandbox Code Playgroud)
PaR*_*RaJ 19
你可以使用Where
并检查是否is
有类型对象的实例!
List<content> rows = PlaceHolder_ForEntries.Controls.Cast<Control>().Where(c => c is TextBox || c is DropDownList)
.Select(txt => new
{
Txt = txt,
Number = new String(txt.ID.SkipWhile(c => !Char.IsDigit(c)).ToArray())
})
.GroupBy(x => x.Number)
.Select(g => new content
{
carclass = g.First(x => x.Txt.ID.StartsWith("DropDownlist_CarClass")).Txt.SelectedValue,
name = g.First(x => x.Txt.ID.StartsWith("TextBox_Name")).Txt.Text,
memberNo = g.First(x => x.Txt.ID.StartsWith("TextBox_MemberNo")).Txt.Text,
points = int.Parse(g.First(x => x.Txt.ID.StartsWith("TextBox_Points")).Txt.Text)
})
.ToList();
Run Code Online (Sandbox Code Playgroud)
Dan*_*Tao 10
AppDeveloper是对的.OfType<T>
过滤掉除以外的所有类型的对象T
; 因此,通过过滤两次,您可以有效地消除列表中的所有对象.
如果你想将这个逻辑(从列表中过滤掉除了两种类型之外的所有类型)包装成可重用的东西,那么没有什么能阻止你实现自己的扩展方法:
using System.Collections;
public static class EnumerableExtensions
{
public static IEnumerable OfType<T1, T2>(this IEnumerable source)
{
foreach (object item in source)
{
if (item is T1 || item is T2)
{
yield return item;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在项目中包含上述类将允许您在应用程序中编写这样的代码:
var textBoxesAndDropDowns = controls.OfType<TextBox, DropDownList>();
Run Code Online (Sandbox Code Playgroud)
若要了解有关扩展方法的详细信息,请参阅有关此主题的MSDN文章.
请注意,由于上面的扩展方法"允许"两种不同的类型,结果仍然是非通用IEnumerable
序列.如果您想将结果视为通用序列(例如,an IEnumerable<Control>
),我建议使用Cast<T>
扩展方法:
var filteredControls = controls.OfType<TextBox, DropDownList>().Cast<Control>();
Run Code Online (Sandbox Code Playgroud)