سؤال

أحتاج إلى تحديد صفوف مميزة من عرض Textfile أدناه.

ملف نصي

 123| one| two| three  <br/>
124| one| two| four <br/>
 125| one |two| three <br/>

يجب أن يكون الإخراج مثل هذا

 123| one| two| three  <br/>
124| one| two| four <br/>

أو

124| one| two| four <br/>
125| one |two| three <br/>

أنا أستخدم هذا الكود لحل هذه المشكلة

var readfile = File.ReadAllLines(" text file location ");
        var spiltfile = (from f in readfile
                    let line = f.Split('|')
                    let y = line.Skip(1)
                    select (from str in y
                            select str).FirstOrDefault()).Distinct()

شكرًا

هل كانت مفيدة؟

المحلول

التباعد غير الواضح في السؤال لا يساعد (خصوصاً حول ال |two|, ، والتي لها مسافات مختلفة عن الباقي، مما يعني أننا بحاجة إلى استخدام التشذيب)، ولكن إليك بعض أساليب LINQ المخصصة التي تقوم بهذه المهمة.لقد استخدمت النوع المجهول كطريقة بسيطة لتسوية المسافات غير المتناسقة (كان بإمكاني أيضًا إعادة بناء سلسلة، لكن بدا ذلك غير ضروري)

لاحظ أنه بدون المسافات الفردية، يمكن أن يكون ذلك ببساطة:

var qry = ReadLines("foo.txt")
        .DistinctBy(line => line.Substring(line.IndexOf('|')));

الكود الكامل:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
static class Program
{
    static void Main()
    {
        var qry = (from line in ReadLines("foo.txt")
                   let parts = line.Split('|')
                   select new
                   {
                       Line = line,
                       Key = new
                       {
                           A = parts[1].Trim(),
                           B = parts[2].Trim(),
                           C = parts[3].Trim()
                       }
                   }).DistinctBy(row => row.Key)
                  .Select(row => row.Line);

        foreach (var line in qry)
        {
            Console.WriteLine(line);
        }
    }
    static IEnumerable<TSource> DistinctBy<TSource, TValue>(
        this IEnumerable<TSource> source,
        Func<TSource, TValue> selector)
    {
        var found = new HashSet<TValue>();
        foreach (var item in source)
        {
            if (found.Add(selector(item))) yield return item;
        }
    }
    static IEnumerable<string> ReadLines(string path)
    {
        using (var reader = File.OpenText(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
}

نصائح أخرى

تحقق من هذا، وهذا سوف يفعل ما تريد القيام به

    static void Main(string[] args)
    {


        string[]  readfile = System.IO.File.ReadAllLines(@"D:\1.txt");
        var strList = readfile.Select(x => x.Split('|')).ToList();            

        IEnumerable<string[]> noduplicates =strList.Distinct(new StringComparer());

        foreach (var res in noduplicates)
            Console.WriteLine(res[0] + "|" + res[1] + "|" + res[2] + "|" + res[3]);
     }

وتنفيذ IEqualityComparer من هنا

class StringComparer : IEqualityComparer<string[]>
{
    public bool Equals(string[] x, string[] y)
    {         
        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x[1].Trim() == y[1].Trim() && x[2].Trim() == y[2].Trim() && x[3].Trim() == y[3].Trim() ;
    }


    public int GetHashCode(string[] data)
    {

        if (Object.ReferenceEquals(data, null)) return 0;        
        int hash1 = data[1] == null ? 0 : data[1].Trim().GetHashCode();

        int hash2 = data[2] == null ? 0 : data[2].Trim().GetHashCode();

        int hash3 = data[3] == null ? 0 : data[3].Trim().GetHashCode();

        return hash1 ^ hash2 * hash3;
    }

}

وسوف يعطيك الإخراج كما كنت تتوقع.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top