2017-09-15 37 views
-1

同樣有什麼辦法來翻譯下面的查詢獲取所有重複開始出現連續

select word 
from string1 
where Left(word, 1) in (
    select Left(word, 1) as firstInitial 
    from string1 
    group by Left(word , 1) 
    having count(*) > 1 
) 

到LINQ,所以當你在運行「雖然金正日一直踢我吃了我的唐恩都樂以極大的趣味「它產生類似於

(miss,Match,Match,Match,miss,miss,miss,Match,Match,miss,Match,Match) 

回答

1

以下解決方案顯示了一種可能的方法。要使用此功能,請務必將MoreLINQ NuGet package添加到您的項目中。

using System; 
using System.Linq; 
using MoreLinq; 

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var input = "While Kim kept kicking I ate my Dunkin donut with great gusto"; 

      var value = input.Split(' '); 

      var lagged = value.Lag(1, (current, previous) => new { current = current?.ToLowerInvariant(), previous = previous?.ToLowerInvariant() }); 
      var leaded = value.Lead(1, (current, next) => new { next = next?.ToLowerInvariant() }); 

      var results = lagged.Zip(leaded, (x, y) => x.current?.FirstOrDefault() == x.previous?.FirstOrDefault() || 
                 x.current?.FirstOrDefault() == y.next?.FirstOrDefault()); 

      Console.WriteLine(string.Join(",", results)); 
      Console.ReadLine(); 
     } 
    } 
} 

基本上代碼拆分串分成多個字,然後着眼於每個單詞(current)和之前(previous)字和之後(next)它。然後它比較current的第一個字母與previousnext的第一個字母。

如果你想返回1/0而不是true/false然後改變只是這行代碼:

var results = lagged.Zip(leaded, (x, y) => (x.current?.FirstOrDefault() == x.previous?.FirstOrDefault() || 
              x.current?.FirstOrDefault() == y.next?.FirstOrDefault()) ? 1 : 0); 
+1

非常感謝你,mjwills。 – user8595118

1

對於這個問題的解決不會像幾行簡單的,但我可以嘗試:

首先,最簡單的但不是那麼優雅for循環方法:

var words = string1.Split(' ').ToList(); 
string[] results = new string[words.Count]; //edited: can use .Count instead of .Count() 
for (int i = 0; i < words.Count; i++) 
{ 
    if (i == words.Count - 1) 
     results[i] = char.ToLower(words[i - 1][0]) == char.ToLower(words[i][0]) ? "Match" : "miss"; 
    else if (i == 0) 
     results[i] = char.ToLower(words[i + 1][0]) == char.ToLower(words[i][0]) ? "Match" : "miss"; 
    else 
    { 
     bool leftMatch = char.ToLower(words[i - 1][0]) == char.ToLower(words[i][0]); 
     bool rightMatch = char.ToLower(words[i + 1][0]) == char.ToLower(words[i][0]); 
     results[i] = (leftMatch || rightMatch) ? "Match" : "miss"; 
    } 
} 

這樣做是通過每個元素,如果左邊或右邊的單詞有相同的首字符,它是一個「匹配」,否則它是「小姐」。對於第一個和最後一個字,它只是需要檢查一個鄰居,而不是2

使用LINQ的Enumerable.Range Method (Int32, Int32),還有?: Operator,這可以簡化爲幾行:

var words = string1.Split(' ').ToList(); 
var results = Enumerable.Range(0, words.Count).Select(i => i == words.Count - 1 ? 
     char.ToLower(words[i - 1][0]) == char.ToLower(words[i][0]) ? "Match" : "miss" : 
     i == 0 ? 
     char.ToLower(words[i + 1][0]) == char.ToLower(words[i][0]) ? "Match" : "miss" : 
     (char.ToLower(words[i - 1][0]) == char.ToLower(words[i][0]) || char.ToLower(words[i + 1][0]) == char.ToLower(words[i][0])) ? "Match" : "miss").ToList(); 

ToList()在最後是可選的,如果你願意,你可以轉換ToArray()

+0

兩者都給我一個錯誤:Cs0121 c#調用之間的模糊以下方法或屬性和 問題是你做一個整數除法(結果也在一個int)和一個int可以隱式轉換爲雙精度和十進制。因此,你需要確保表達結果在其中之一;雙倍可能是你想要的。 – user8595118

+0

謝謝你,Keyur PATEL。好的解決方案。我從你那裏學到很多,夥計們。偉大的網站! – user8595118