简体   繁体   English

为什么我不能遍历匿名类型

[英]why I can't iterate through anonymous type

ControllerAction: ControllerAction:

{

    AddLeadSourcesModel model = new AddLeadSourcesModel();
    List<LeadSourceModel> models = LeadSourceModel.Get()
                                                  .Where(c => c.Active == true)
                                                  .ToList();
    var A = LeadSourceModel.GetSubLeadSources(leadSourceID);
    model.LeadSources = new SelectList(
        LeadSourceModel.Get().Where(c => c.Active == true), 
        "LeadSourceID", 
        "Name", 
        models.Where(c => c.LeadSourceID == leadSourceID).FirstOrDefault().LeadSourceID
    );

    model.SubLeadSources = new SelectList(A, "LeadSourceID", "Name");

    var flattenedData = model.SubLeadSources.SelectMany(subLead => model.LeadSources, (n, a) => new { n,a });

    return PartialView("_AddLeadSources", flattenedData.ToList());

}

View: 视图:

<table>
    <tr>
        <td>Lead Source</td>
        <td>Sub Lead Source</td>
        <td>Action</td>
    </tr>
    @foreach(var item in Model)
    {
    <tr>
        <td>@item.a</td>
        <td>@item.n</td>
        <td><a href="#">Remove</a></td>
    </tr>
    }
</table>

My problem is when I get to the partial it says it expects a different type but I've not told it a type. 我的问题是,当我到达局部视图时,它说它期望使用其他类型,但是我没有告诉它一种类型。 And I technically don't know the type coz it's annoymous 而且我从技术上不知道类型coz很讨厌

How do I get this to work? 我该如何工作?

Somewhere you've defined class PartialView with a constructor that takes two parameters. 在某个地方,您已经PartialView带有两个参数的构造函数定义了PartialView类。

Alas you forgot to give us the signature of this constructor, but I think that the first one is a string, the second one is list / sequence / collection of some defined type. las,您忘记给我们此构造函数的签名,但是我认为第一个是字符串,第二个是某些已定义类型的列表/序列/集合。 I expect something like: 我期望类似:

class PartialView
{
     public PartialView(string txt, List<MyType> items) {...}
}

If you want to use this constructor you'll have to provide a List<MyType> . 如果要使用此构造函数,则必须提供List<MyType> To do this, use Select before you create FlattenedData: 为此,请在创建FlattenedData之前使用Select

List<MyType> flattenedData = model.SubLeadSources
    .SelectMany(subLead => model.LeadSources, (n, a) => new { n,a })
    .Select(item => new MyType()
    {
        // fill the MyType properties, something like:
        N= item.n
        A = item.a,
    };

Now you can use the constructor. 现在您可以使用构造函数了。

PartialView partialView = new PartialView("_AddLeadSources", flattenedData);

Extension Function 扩展功能

If you'll convert quite often some anonymous type to a sequence of MyType and a PartialView, consider creating an extension function that will take as input a sequence of any (anonymous) type, a string, and two lambda expressions that will tell you which values of the anonymous types are the A and N. 如果您经常将某些匿名类型转换为MyType和PartialView的序列,请考虑创建一个扩展函数,该函数将使用任何(匿名)类型的序列,字符串和两个lambda表达式作为输入,以告诉您匿名类型的值是A和N。

See extension methods demystified 查看神秘化的扩展方法

We already know that MyType has properties like A and an N, where you'd like to put your anonymous a and n in. Let's assume they have non-generic type Atype and Ntype, for instance int, or DateTime, or MyElaborateClass, like this: 我们已经知道MyType具有A和N之类的属性,您想在其中放置匿名a和n。假设它们具有非通用类型Atype和Ntype,例如int或DateTime或MyElaborateClass,例如这个:

class MyType
{
     public Atype A {get; set;}
     public Btype B {get; set;}
}

Extension class for PartialView: PartialView的扩展类:

static class PartialViewExtension
{
    // Creates one PartialView object from a sequence of MyType objects and a text
    public static PartialView ToPartialView(this IEnumerable<MyType> source, string text)
    {
         return  new PartialView(text, source.ToList());
    }

    // uses the source + predicates to create a sequence of MyType objects
    public static IEnumerable<MyType> ToMyType<TSource>(this IEnumerable<TSource> source,
        Func<TSource, Atype> aSelector,
        Func<TSource, Ntype> nSelector)
    {
         return source.Select(sourceItem => new MyType()
         {
              A = aSelector(sourceItem),
              B = bSelector(sourceItem),
         });
    }
}

Usage would be like: 用法如下:

var flattenedData = model.SubLeadSources
    .SelectMany(subLead => model.LeadSources, (n, a) => new { n,a });

PartialView partialView = flattenedData
      .ToMyType(flattenedInput => flattenedInput.n,
                flattendInput => flattenedInput.a)
      .ToPartialView("_AddLeadSources");

Now that we are already creating extension functions, lets combine these two into one, that will call the other two: 现在我们已经创建了扩展功能,让我们将这两个功能合并为一个,将调用另外两个功能:

public static PartialView ToPartialView<TSource>(this IEnumerable<TSource> source,
    string text,
    Func<TSource, Atype> aSelector,
    Func<TSource, Ntype> nSelector)
{
    return source.ToMyType(aSelector, bSelector).ToPartialView(text);
}

Usage: 用法:

var flattenedData = model.SubLeadSources
    .SelectMany(subLead => model.LeadSources, (n, a) => new { n,a });

PartialView = flattenedData.ToPartialView("_AddLeadSources",
    flattenedInput => flattenedInput.a,
    flattenedInput => flattenedInput.n);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 联合JsonResult如果项目是匿名类型,为什么我无法通过动态访问 - Uniting JsonResult why can't I get access through dynamic if the items are an anonymous type 如何遍历匿名类型的列表 - How to iterate through a list of an anonymous type 为什么我不能在C#中创建一个匿名类型的列表<T>? - why can't I create a list<T> of anonymous type in C#? 为什么我不能通过dynamic关键字访问从函数返回的匿名类型的属性? - Why can't I access properties of an anonymous type returned from a function via the dynamic keyword? 为什么不能在其创建方法之外使用匿名类型? - Why can't an Anonymous Type be used outside the method it's created in? 为什么我不能在 C# 中将匿名类型作为泛型返回,而我可以对方法参数执行相同的操作? - why I can't return anonymous type as generic in C#, while I can do the same for method parameters? 我可以在列表中使用匿名类型吗? <T> 而不是帮助类? - Can I use an anonymous type in a List<T> instead of a helper class? 为什么我不能在匿名方法中的while循环中使用break? - Why can't I use break in a while loop in an anonymous method? ASP.NET,C#和匿名类型 - 在手动构建匿名类型时迭代DataTable - ASP.NET, C# and Anonymous Types - Iterate through a DataTable while Manually Building an Anonymous Type 如果匿名类型应该是不可变的,为什么可以更改它? - Why can the anonymous type be changed if it is supposed to be immutable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM