找到两个句子之间匹配的第一个单词

时间:2016-12-03 11:10:01

标签: c#

我有一个计数。我有来自数据库的数据。我想根据传入的动词找到第一个匹配的单词。

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 个答案:

答案 0 :(得分: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);
相关问题