C# 具有复杂模型映射的Linq查询

C# 具有复杂模型映射的Linq查询,c#,.net,linq,C#,.net,Linq,我正在尝试从我的webapi映射到我的解决方案中的某个ICollection 这是一个HttpClient:var currentNotifications=wait client.GetUsersAsync() 例如,如果我有一个模型: public class UserNotificationTypeDeliveryChoice { public DeliveryType DeliveryType { get; set; } public NotificationGroup No

我正在尝试从我的webapi映射到我的解决方案中的某个ICollection 这是一个HttpClient:
var currentNotifications=wait client.GetUsersAsync()

例如,如果我有一个模型:

public class UserNotificationTypeDeliveryChoice
{
   public DeliveryType DeliveryType { get; set; }
   public NotificationGroup NotificationGroup{ get; set; }
}

public class DeliveryType
{
   public byte DeliveryTypeId { get; set; }
   public string Name { get; set; }
}

public class NotificationGroup
{
   public byte NotificationGroupId { get; set; }
   public string Name { get; set; }
}

和要映射到的模型:

public class NotificationListViewModel
{
  public DeliveryType DeliveryType { get; set; }
  public NotificationGroup NotificationGroup { get; set; }
}
public class DeliveryType
{
  public byte DeliveryTypeId { get; set; }
  public string Name { get; set; }
}

public class NotificationGroup
{
   public int NotificationGroupId { get; set; }
   public string Name { get; set; }
}
因此,我需要从我的webapi映射到一个
ICollection currentNotifications

列表通知列表视图模型

Myexample看起来像:

var res = currentNotifications.ToList().ForEach(x => notificationListViewModel.Add(new NotificationListViewModel()
{
    DeliveryType = new DeliveryType() {DeliveryTypeId = x.DeliveryType.DeliveryTypeId.Value, Name = x.DeliveryType.Name },
    NotificationGroup = new NotificationGroup() { NotificationGroupId = x.NotificationGroup.NotificationGroupId.Value, Name = x.NotificationGroup.Name }
}));
但是,它向我报告了一个错误:
CS0815无法将void赋值给隐式类型的变量

Ofc,如果您有更好的解决方案,如果没有ForEach,我将不胜感激。

问题在于:

var res = currentNotifications.ToList().ForEach(...);
ForEach()
返回
void
,因此您试图将
void
分配给
res
。那不行

首先存储列表,然后对每个进行
ForEach

var res = currentNotifications.ToList();

res.ForEach(...);
或者干脆将
res
一起删除,因为您将结果存储在
notificationListViewModel

currentNotifications.ToList().ForEach(...);
这取决于您是否仍然需要
res


作为一种补充和进一步的改进,您正在以不同的方式执行LINQ
Select()
。使用LINQ等效物:

notificationListViewModel = currentNotifications.Select(x => new NotificationListViewModel() { ... });
ForEach()
不返回任何内容。 您可以使用
Select()
调用来代替:

var result = currentNotifications.Select(x => new NotificationListViewModel
{
    DeliveryType = new DeliveryTypeViewModel
    {
        DeliveryTypeId = x.DeliveryType.DeliveryTypeId,
        Name = x.DeliveryType.Name
    },
    NotificationGroup = new NotificationGroupViewModel
    {
        NotificationGroupId = x.NotificationGroup.NotificationGroupId,
        Name = x.NotificationGroup.Name
    }
});

我在所有视图模型类名中添加了
ViewModel
后缀,以使其更具可读性。

您以前使用过automapper吗?我认为您不能使用automapper将字节直接映射到intIm,但我更希望将字节强制转换为intIm。如果您的源示例不包含任何集合或列表,那么失败的赋值在哪里?如果您使用。BitConverter.ToInt32(x.NotificationGroup.NotificationGroupId)而不是Value使用All而不是foreach currentNotifications.ToList().Alll(…);我正在返回res。所以我真的不需要res。谢谢!我从
.Select()
开始,但是我已经迷失在lambda操作符和新语句中了