2016-12-03 104 views
0

我有一個計數。我有數據庫中的數據。我想根據輸入的動詞找到第一個匹配的單詞。找到兩個句子之間匹配的第一個單詞

public class HesapPlanı 
{ 
    public string hesapKodu { get; set; } 
    public string hesapAdi { get; set; } 
} 

查詢

//Actually they come from the database. 
List<HesapPlanı> hesap = new List<HesapPlanı>(); 
hesap.Add(new HesapPlanı { hesapKodu = "100 01 001", hesapAdi = "Kasa" }); 
hesap.Add(new HesapPlanı { hesapKodu = "120 01 001", hesapAdi = "CARİ KART" }); 
hesap.Add(new HesapPlanı { hesapKodu = "340 01 001", hesapAdi = "AFYON ÖDEMESİ" }); 
hesap.Add(new HesapPlanı { hesapKodu = "350 01 001", hesapAdi = "Kasa" }); 
hesap.Add(new HesapPlanı { hesapKodu = "360 02 001", hesapAdi = "STOPAJ ÖDEMESİ" }); 


string kalan = null; 
string[] liste1 = "GELEN EFT - CARİ ÖDEMESİ".Split(new char[] { ' ', '-' }); 
string[] liste2 = null; 
string sorgu = null; 

foreach (var item in hesap.Select(h => h.hesapAdi)) 
{ 
    liste2 = item.Split(new char[] { ' ', '-' }); 
    var kume = liste1.Intersect(liste2); 
    sorgu = liste2.Intersect(kume).FirstOrDefault(); 
    if (!string.IsNullOrWhiteSpace(sorgu)) 
    { 
    kalan = sorgu; 
    } 
} 
Console.WriteLine(hesap.Where(m => m.hesapAdi.Contains(kalan)).FirstOrDefault().hesapKodu); 

這些操作的結果是: 「340 01 001」

實際應該是這個結果: 「120 01 001」

回答

1

因爲在kalan="ÖDEMESİ"之後循環。你從列表中選擇的是hesapAdi proparty有ÖDEMESİ這個詞。並導致兩個線路hesapKodu=340 01 001hesapKodu=360 02 001.FirstOrDefault()回報hesapKodu=340 01 001

您可以使用

//Actually they come from the database. 
      List<HesapPlanı> hesap = new List<HesapPlanı>(); 
      hesap.Add(new HesapPlanı { hesapKodu = "100 01 001", hesapAdi = "Kasa" }); 
      hesap.Add(new HesapPlanı { hesapKodu = "120 01 001", hesapAdi = "CARİ KART" }); 
      hesap.Add(new HesapPlanı { hesapKodu = "340 01 001", hesapAdi = "AFYON ÖDEMESİ" }); 
      hesap.Add(new HesapPlanı { hesapKodu = "350 01 001", hesapAdi = "Kasa" }); 
      hesap.Add(new HesapPlanı { hesapKodu = "360 02 001", hesapAdi = "STOPAJ ÖDEMESİ" }); 




     List<string> kalanList =new List<string>(); 
     string[] liste1 = "GELEN EFT - CARİ ÖDEMESİ".Split(new char[] { ' ', '-' }); 
     string[] liste2 = null; 
     string sorgu = null; 

     foreach (var item in hesap.Select(h => h.hesapAdi)) 
     { 
      liste2 = item.Split(new char[] { ' ', '-' }); 
      var kume = liste1.Intersect(liste2).ToList(); 
      sorgu = liste2.Intersect(kume).FirstOrDefault(); 
      if (!string.IsNullOrWhiteSpace(sorgu)) 
      { 
       kalanList .Add(sorgu); 
      } 
     } 


     List<HesapPlanı> resultList = new List<HesapPlanı>(); 
     foreach (var item in kalanList) 
     { 
      resultList.Add(hesap.Where(m => m.hesapAdi.Contains(item)).FirstOrDefault()); 
     } 


     Console.WriteLine(resultList.FirstOrDefault().hesapKodu); 
+0

結果列表。我想得到一個結果:「340 01 001」。結果:「CARİ」返回。 –

相關問題