【发布时间】:2016-01-06 15:54:57
【问题描述】:
我试图从表中拉出UserId's,但是当我拉出它时,它以包含字符串数组的字符串数组的形式出现。
如何将其展平以获得字符串数组?我尝试过使用SelectMany(i => i.UserId),但我得到了错误
无法将类型'System.Collections.Generic.IEnumerable
'隐式转换为'System.Collections.Generic.IEnumerable '
这是为什么?为什么它认为结果是char类型?
[DataContract]
public class AdminDashboardData
{
[DataMember]
public IEnumerable<string> Users { get; set; }
}
public IEnumerable<string> GetUserIds(IEnumerable<int> groupIds)
{
using(GameDbContext entityContext = new GameDbContext())
{
return entityContext.InvestigatorGroupUsers
.Include(i => i.InvestigatorGroup)
.Where(i => i.InvestigatorGroup.IsTrashed == false)
.Where(i => groupIds.Contains(i.InvestigatorGroupId))
.Select(i => i.UserId).Distinct().ToFullyLoaded();
}
}
public class InvestigatorGroupUser
{
public int InvestigatorGroupUserId { get; set; }
public int InvestigatorGroupId { get; set; }
public string UserId { get; set; }
public InvestigatorGroup InvestigatorGroup { get; set; }
}
public AdminDashboardData GetDashboardData(string userId)
{
IGamePlayedRepository GamePlayedRepo = _GamePlayedRepo ?? new GamePlayedRepository();
// Get the investigator groups from the userid
IEnumerable<int> groupIds = GroupUserRepo.GetGroupIds(userId);
AdminDashboardData data = new AdminDashboardData();
if (!groupIds.IsNullOrEmpty())
{
data.Users = GroupUserRepo.GetUserIds(groupIds);
}
return data;
}
public static IEnumerable<T> ToFullyLoaded<T>(this IEnumerable<T> enumerable)
{
return enumerable.ToList();
}
【问题讨论】:
-
您的
UserID是string,而不是字符串数组。可以枚举它以获得char类型的字符。这解释了异常。 SelectMany 不是必需的。 -
方法
ToFullyLoaded()是做什么的? -
您的代码编写方式应该返回一个字符串数组,因为 UserId 是一个字符串,而不是字符串数组。我猜使用 selectmany 是选择字符串的字符数组并将它们展平为字符数组。
-
您在用户 ID 上执行
SelectMany(i => i.UserId),其中userid似乎是string,因此 SelectMany 返回IEnumerable<char>。请正确解释这一点“当我拉它时它以字符串数组的形式出现”. -
我要做的是获取所有
UserId's并将其存储到我的数据合约中的IEnumerable<string>中。ToFullyLoaded()与ToList()相同
标签: c# asp.net-mvc entity-framework linq lambda