C# 安格尔阵列。这只是一个类似字符串[]arr={“s_0001”、“s_0002”、“s_0003”、“sa_0004”、“sa_0005”、“sab_0006”、“sab_0007”…}的例子,在这里使用正则表达式是不受欢迎的?这正是正则表达式的用途,解析

C# 安格尔阵列。这只是一个类似字符串[]arr={“s_0001”、“s_0002”、“s_0003”、“sa_0004”、“sa_0005”、“sab_0006”、“sab_0007”…}的例子,在这里使用正则表达式是不受欢迎的?这正是正则表达式的用途,解析,c#,arrays,duplicate-removal,C#,Arrays,Duplicate Removal,安格尔阵列。这只是一个类似字符串[]arr={“s_0001”、“s_0002”、“s_0003”、“sa_0004”、“sa_0005”、“sab_0006”、“sab_0007”…}的例子,在这里使用正则表达式是不受欢迎的?这正是正则表达式的用途,解析一个字符串:)如果你担心性能,你可以把正则表达式拉到一个变量中,并在Select之前编译它。然后它很快就失明了。但这是最好的微优化,我不推荐它,除非它运行得很慢 string[] arr = new[] { "s_0001", "s_0002"


安格尔阵列。这只是一个类似字符串[]arr={“s_0001”、“s_0002”、“s_0003”、“sa_0004”、“sa_0005”、“sab_0006”、“sab_0007”…}的例子,在这里使用正则表达式是不受欢迎的?这正是正则表达式的用途,解析一个字符串:)如果你担心性能,你可以把正则表达式拉到一个变量中,并在Select之前编译它。然后它很快就失明了。但这是最好的微优化,我不推荐它,除非它运行得很慢
string[] arr = new[] { "s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007" };
s_0001
sa_0004
sab_0006
//Loop through the XML files in the Directory and get
//the objectName and GUID of each file
string[] arr_xmlFiles = Directory.GetFiles(Dir, "*.xml");   //Array with all XML Files in the Directory

foreach (string xmlFile in arr_xmlFiles)
{
    try
    {
        //Get the XMLs Name
        XDocument xmlF = XDocument.Load(xmlFile);
        string objectName = xmlF.Root.Name.ToString();

        //Get the XMLs GUID
        XElement oDcElement = xmlF.Root.FirstNode as XElement;
        Guid oGuid = new Guid(oDcElement.Attribute("DataclassId").Value);

        //Prints out the results 
        Console.WriteLine(" " + objectName + "    " + oGuid);
    }
    catch (XmlException) { }
}
CM_Commands [0ee2ab91-4971-4fd3-9752-cf47c8ba4a01].xml    
CM_Commands [1f627f72-ca7b-4b07-8f93-c5750612c209].xml
public class MyClass 
{
    public string ObjectName { get; set; }
    public string Guid { get; set; }
    public string FileName { get; set; }
}
/* start of query unchanged ... */
select new MyClass
{
    ObjectName = split[0],
    Guid = split[1],
    FileName = f.FullName
};
string path = @"C:\Foo\Bar"; // your path goes here
var dirInfo = new DirectoryInfo(path);

// DirectoryInfo.GetFiles() returns an array of FileInfo[]
// FileInfo's Name property gives us the file's name without the full path
// LINQ let statement stores the split result, splitting the filename on spaces
// and dots to get the objectName, and Guid separated from the file extension.
// The "select new" projects the results into an anonymous type with the specified
// properties and respectively assigned values. I stored the fullpath just in case.
var query = from f in dirInfo.GetFiles("*.xml")
            let split = f.Name.Split(new[] { ' ', '.' })
            select new 
            {
                ObjectName = split[0],
                Guid = split[1],
                FileName = f.FullName
            };

// Now that the above query has neatly separated the ObjectName, we use LINQ
// to group by ObjectName (the group key). Multiple files may exist under the same
// key so we then select the First item from each group.
var results = query.GroupBy(o => o.ObjectName)
                   .Select(g => g.First());

// Iterate over the results using the projected property names.
foreach (var item in results)
{
    Console.WriteLine(item.FileName);
    Console.WriteLine("ObjectName: {0} -- Guid {1}", item.ObjectName, item.Guid);
}
string[] arr = {"s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007"};

var query = arr.GroupBy(s => s.Substring(0, s.IndexOf('_')))
               .Select(g => g.First());

foreach (string s in query)
    Console.WriteLine(s);    // s_0001, sa_0004, sab_0006
var query = arr.Select(s => s.Substring(0, s.IndexOf('_')))
               .Distinct();    // s, sa, sab
string[] arr = new[] { "s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007" };

var elementsByPrefix = arr.Select(s =>
{
    int indexOfUnderscore = s.IndexOf('_');
    if (indexOfUnderscore >= 0)
    {
        return new { Prefix = s.Substring(0, indexOfUnderscore), Suffix = s.Substring(indexOfUnderscore + 1, s.Length - (indexOfUnderscore + 1)) };
    }
    else
    {
        return new { Prefix = s, Suffix = string.Empty };
    }
}).GroupBy(item => item.Prefix);

foreach (var element in elementsByPrefix)
{
    Console.WriteLine("{0}_{1}", element.Key, element.First().Suffix);
}
string[] arr = {"s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007"};

arr.Select(a => Regex.Match(a,@"([A-Za-z]+)_([0-9]+)").Groups[1].ToString()).Distinct();
Dictionary<string,string> lettersToRecords = new Dictionary<string,string>();
arr.Foreach((record) =>
    {
        string letters = record.Split('_')[0];
        if(!lettersToRecords.Keys.Contains(letters))
        {
            lettersToRecords[letters] = record;
        }
    });
class YourStringComparer : System.Collections.Generic.IEqualityComparer<string[]>
{
    public bool Equals(string[] x, string[] y)
    {
        throw new NotImplementedException(); // not used here
    }

    public int GetHashCode(string[] obj)
    {
        return obj.First().GetHashCode();
    }
}

string[] arr = new[] { "s_0001", "s_0002", "s_0003", "sa_0004", "sa_0005", "sab_0006", "sab_0007" };

var r = arr.Select(s => s.Split('_')).Distinct(new YourStringComparer());
// "s_0001", "sa_0004", "sab_0006"