[英]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);
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.