【问题标题】:Flatten IEnumerable<string> - SelectMany issue展平 IEnumerable<string> - SelectMany 问题
【发布时间】:2016-01-06 15:54:57
【问题描述】:

我试图从表中拉出UserId's,但是当我拉出它时,它以包含字符串数组的字符串数组的形式出现。

如何将其展平以获得字符串数组?我尝试过使用SelectMany(i =&gt; 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();
}

【问题讨论】:

  • 您的UserIDstring,而不是字符串数组。可以枚举它以获得char 类型的字符。这解释了异常。 SelectMany 不是必需的。
  • 方法ToFullyLoaded()是做什么的?
  • 您的代码编写方式应该返回一个字符串数组,因为 UserId 是一个字符串,而不是字符串数组。我猜使用 selectmany 是选择字符串的字符数组并将它们展平为字符数组。
  • 您在用户 ID 上执行 SelectMany(i =&gt; i.UserId),其中 userid 似乎是 string,因此 SelectMany 返回 IEnumerable&lt;char&gt;。请正确解释这一点“当我拉它时它以字符串数组的形式出现”.
  • 我要做的是获取所有UserId's 并将其存储到我的数据合约中的IEnumerable&lt;string&gt; 中。 ToFullyLoaded()ToList() 相同

标签: c# asp.net-mvc entity-framework linq lambda


【解决方案1】:

剥离其本质(并替换我手头的数据库),我们有这个易于使用 LinqPad 的代码块:

void Main()
{
    GetDashboardData().Dump();
}

public IEnumerable<string> GetUserIds()
{
        return this.Peoples
            .Where(i => i.HasPhoto == false)
            .Select(i => i.FirstName).Distinct().ToFullyLoaded();
}

public IEnumerable<string> GetDashboardData()
{
    var Users = this.GetUserIds();
    return Users;
}

static class Ext
{
    public static IEnumerable<T> ToFullyLoaded<T>(this IEnumerable<T> enumerable)
    {
        return enumerable.ToList();
    }
}

这将显示一维字符串列表。不管是什么问题,都不在这段代码中。

请记住,字符串被视为IEnumerable&lt;char&gt;,因此您确实得到了“数组(字符)数组”,但您可以将其视为字符串数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    相关资源
    最近更新 更多