【发布时间】:2017-06-27 07:38:26
【问题描述】:
我正在尝试填充元组列表,其中元素的数量由 Enumerable 确定。 我试图遍历元组列表中的枚举:
var items = UnitOfWork.GetAll<Table>().Where(x => tableIds.Contains(x.Id));
var tupleList = new List<Tuple<string, string>> {
foreach (var item in items)
{
new Tuple<string, string>(item.A1, item.A2),
new Tuple<string, string>(item.A1, item.A2)
{
};
但这似乎是不允许的(-> 右大括号处的红色摆动线))。 从元组列表外部循环不会公开元组列表以供进一步使用。 任何帮助,将不胜感激。问候,马努
在 Tims 的建议下,我的代码现在看起来:
public IEnumerable<EmailListModel> Get()
{
IIndireKatPrincipal indireKatPrincipal = identityStorage.GetPrincipal();
var groupId = indireKatPrincipal.GroupId;
var araIds = UnitOfWork.GetAll<UserGroup>()
.Where(group => group.Id == groupId)
.SelectMany(group => group.S_ARA).Select(ara => ara.Id);
var aras = UnitOfWork.GetAll<S_ARA>().Where(x => araIds.Contains(x.Id)).AsEnumerable();
List<Tuple<string, string>> tupleList = aras
.Select(ara => new Tuple<string, string>(ara.IEVName, ara.IEVEmail))
.ToList();
var emailListModel = tupleList.Select(entity => new EmailListModel()
{
DisplayAddress = entity.Item1,
EmailAddress = entity.Item2
}).ToList();
return emailListModel;
}
它现在完美运行(在 var aras = ... 添加 .AsEnumerable() 之后)。
【问题讨论】:
-
您确定错误来自这部分代码吗?我无法观察到您在 this fiddle 中描述的相同行为
-
@Icepickle:我也尝试了另一个 Enumerable,我确信它会产生正确的记录 -> 调试模式中的相同错误 -> NotSupportedException,在 LINQ to Entities 中只有无参数构造函数/支持初始化器
-
我想您的问题中缺少此信息,但是我看不出它与您提到的错误行有什么关系
-
@Icepickle:现在我用您的 ItemDataSource 进行了尝试,但没有出现错误。所以我的 ItemDataSource (var aras = ...) 中一定有错误。我会努力的。
-
@Icepickle:我发现了问题。我必须将 .AsEnumerable() 附加到我的 ItemDataSource (var aras = ...)。
标签: c#