多个OfType Linq?

时间:2013-03-16 17:10:32

标签: c# linq

我有一个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();
}

以下是创建控件的方法。

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

5 个答案:

答案 0 :(得分:17)

您可以使用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();

答案 1 :(得分:10)

AppDeveloper is rightOfType<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>();

要详细了解扩展方法,请参阅the MSDN article on the subject

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

var filteredControls = controls.OfType<TextBox, DropDownList>().Cast<Control>();

答案 2 :(得分:2)

我没有彻底阅读这个问题,但从标题所暗示的,你可以通过以下方式实现这一行为:

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()));

在行动here中查看。

答案 3 :(得分:0)

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

答案 4 :(得分: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) });