16

我有一个 linq 查询,它选择占位符中的所有文本框并使用结构将它们添加到列表中。我需要扩展此功能以获取 DropDownList 的 selectedvalue 我很确定我做错了,因为当我调试该方法时,列表计数为 0。

我自己的猜测是声明 2OfType<>()是错误的,但我对 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();
}

这是创建控件的方法。

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 />"));
    }
}
4

5 回答 5

21

您可以使用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();
于 2013-03-16T17:13:05.873 回答
11

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;
            }
        }
    }
}

在您的项目中包含上述类将允许您在应用程序中编写如下代码:

var textBoxesAndDropDowns = controls.OfType<TextBox, DropDownList>();

要了解有关扩展方法的更多信息,请参阅有关该主题的 MSDN 文章

请注意,由于上面的扩展方法“引入”了两种不同的类型,因此结果仍然是非泛型IEnumerable序列。如果您想将结果视为通用序列(例如, an IEnumerable<Control>),我建议使用Cast<T>扩展方法:

var filteredControls = controls.OfType<TextBox, DropDownList>().Cast<Control>();
于 2013-03-16T17:27:17.560 回答
3

我没有彻底阅读这个问题,但从标题的含义来看,您可以通过以下方式实现该行为:

var collection = new object[] { 5, "4545",  'd', 54.5 , 576 };

var allowedTypes = new[] { typeof(string), typeof(int) }; 
var result = collection
 .Where(item => allowedTypes.Contains(item.GetType()));

在这里查看它的实际应用。

于 2017-02-19T05:29:48.557 回答
0

将@Shimmys 回答为扩展方法:

/// <param name="wantedTypes">--- Sample: --- new Type[] { typeof(Label), typeof(Button) }</param>
public static IEnumerable OfTypes(this IEnumerable collection, Type[] wantedTypes)
{
    if (wantedTypes == null)
        return null;
    else
        return collection.Cast<object>().Where(element => wantedTypes.Contains(element.GetType()));
}

用法:

// List of 3 different controls
List<object> controls = new List<object>(new object[] { new Label(), new Button(), new TextBox() });

// Get all labels and buttons
var labelsAndButtons = controls.OfTypes(new Type[] { typeof(Label), typeof(Button) });
于 2017-03-31T11:32:55.850 回答
-1

您的问题在于OfType<>().OfType<>()您使用不同类型过滤了两次

于 2013-03-16T17:14:47.633 回答