2009-10-27 29 views
1

我需要從下面的Textfile中選擇不同的行。跳過第一列並與其他列不同

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/> 

OR

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() 

感謝

+1

我已經按原樣縮進了樣本行,但請您澄清間距?特別是:最後一個示例包含不在源代碼中的間距。 –

回答

1

在提問的間距不清不利於(尤其|two|,它具有不同的間距比其他人,這意味着我們需要使用修整左右),但這裏的一些自定義的LINQ方法做這項工作。我已經完全使用anon類型作爲平坦化不一致間距的簡單方法(我也可以重建一個字符串,但似乎沒有必要)

請注意,沒有奇數間距,這可以很簡單:

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; 
      } 
     } 
    } 
} 
0

看看這個,這會做你想做的事

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; 
    } 

} 

它將給ü輸出與您預期。