在 WriteLine 中使用 Distinct()

Using Distinct() in WriteLine

我有一个 C# 控制台应用程序。我得到输出

System.Linq.Enumerable+<DistinctIterator>d__811[System.Char]`

当我执行控制台应用程序时。

我想要实现的是输出 projectIds(字符串),避免输出重复项。

这是我的代码。

using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))
using (StreamWriter file = new StreamWriter(fs))
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,NumberOfSubjects,ProjectStartDate,ProjectEndDate,URL");
        for (int s = 0; s < pr.Length; ++s)
        {
            string[] userIDs= new string[] { "" };
                userIDs = db.GetList(pr[s].ProjectID);                
                file.WriteLine("{0},\"{1}\",{2},{3},{4},{5},{6}", pr[s].ProjectID.Distinct(), pr[s].ProjectTitle, pr[s].PublishStatus, userIDs.Length, 
                pr[s].ProjectStartDate,  pr[s].ProjectEndDate,   url[i].ToString());

    }
}

这里有输出和预期输出

Distinct() 仍然是 return 值的集合,它只是一个删除了所有重复值的集合。要在之后生成字符串,您应该使用 string.Join:

string.Join(", ", pr[s].ProjectID.Distinct());

这会将 return 项目 ID 作为逗号分隔列表。

这是你想要做的吗?

using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))
using (StreamWriter file = new StreamWriter(fs))
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,NumberOfSubjects,ProjectStartDate,ProjectEndDate,URL");
    foreach(proj in pr.GroupBy(p=>p.ProjectID).Select(p=>p.First()))
    {
        var userIDs = db.GetList(proj.ProjectID);                
        file.WriteLine("{0},\"{1}\",{2},{3},{4},{5},{6}",
           proj.ProjectID,
           proj.ProjectTitle,
           proj.PublishStatus,
           userIDs.Length.ToString(NumberFormatInfo.InvariantInfo),
           proj.ProjectStartDate.ToString("d",DateTimeFormatInfo.InvariantInfo),
           proj.ProjectEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo),
           url[i].ToString());
    }
}

IF 无论数据类型 pr 是什么,正确实现 IEquatable<T>,您可以轻松使用 distinct:

using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))
using (StreamWriter file = new StreamWriter(fs))
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,NumberOfSubjects,ProjectStartDate,ProjectEndDate,URL");
    foreach(proj in pr.Distinct())
    {
        var userIDs = db.GetList(proj.ProjectID);                
        file.WriteLine("{0},\"{1}\",{2},{3},{4},{5},{6}",
           proj.ProjectID,
           proj.ProjectTitle,
           proj.PublishStatus,
           userIDs.Length.ToString(NumberFormatInfo.InvariantInfo),
           proj.ProjectStartDate.ToString("d",DateTimeFormatInfo.InvariantInfo),
           proj.ProjectEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo),
           url[i].ToString());
    }
}

假设 prProject 类型,您可以像这样编写自定义 IEqualityComparer<T>

class ProjectEqualityComparer : IEqualityComparer<Project>
{
    #region IEqualityComparer<Project> Members

    public bool Equals(Project x, Project y)
    {
        return x.ProjectID.Equals(y.ProjectID);
    }

    public int GetHashCode(Project obj)
    {
        return obj.ProjectID.GetHashCode();
    }

    #endregion
}
using (FileStream fs = new FileStream(outCsvFile, FileMode.Append, FileAccess.Write))
using (StreamWriter file = new StreamWriter(fs))
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,NumberOfSubjects,ProjectStartDate,ProjectEndDate,URL");
    foreach(proj in pr.Distinct(new ProjectEqualityComparer()))
    {
        var userIDs = db.GetList(proj.ProjectID);                
        file.WriteLine("{0},\"{1}\",{2},{3},{4},{5},{6}",
           proj.ProjectID,
           proj.ProjectTitle,
           proj.PublishStatus,
           userIDs.Length.ToString(NumberFormatInfo.InvariantInfo),
           proj.ProjectStartDate.ToString("d",DateTimeFormatInfo.InvariantInfo),
           proj.ProjectEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo),               url[i].ToString());
    }
}