簡體   English   中英

C#:比較List中的字符串 <string> 在單獨的List中的對象中使用字符串屬性 <Object> 使用Obj.getName()

[英]C#: Comparing strings in a List<string> with string attributes in Objects in a seperate List<Object> using Obj.getName()

我正在寫一個程序,我有一個卡片組(MyDeck對象),其中包含一個列表(卡類型列表),其中是我的卡(卡對象)。 還有一個雜志(雜志對象),其中包含待售卡片列表(類型字符串列表)。 我想把雜志中的卡片添加到我的套牌中,只要我還沒有卡片。 困難的部分是我的卡片是對象,而雜志中的那些是字符串,所以我必須調用mycard.getCardName()來獲取我的卡片的名稱和雜志中的卡片我可以簡單地在列表中運行因為它們只是一個字符串列表。

這是我的代碼,問題是只有雜志中的前兩張卡被添加到我的套牌中。

也許我正在接近這個問題。

public static class MyDeck
{
    public static List<Card> Cards = new List<Card>();
    //Other Members
}

public class Card
{
    //associated memebers   
}

public class Magazine
{
    public List<string> MagCards = new List<string>();
    //Other Members
}


private void AddCardsToMyDeck(List<string> MagCards)
{
    //If I have no cards in my deck then add the first one on the magazine to my deck
    if(MyDeck.MyCards.Count == 0)
    {
       Card c = new Card(MagCards[0]);
       MyDeck.MyCards.Add(c);
    }

    //for each name of each card in the magazine       
    foreach(string MagCard in MagCards)
    {
      //for each card in my deck
      foreach (Card crd in MyDeck.MyCards)
      {
         //if the my card is in the magazine then don't add it, else add it
         if (MagCards.Contains(crd.getCardName()))
         {
            break;
         }
         else
         {
            Card cd = new Card(MagCard);
            MyDeck.MyCards.Add(cd);
         }
      }
    }
}


// NOTE: MyDeck.Cards is empty
Magazine.MagCards.Add("B. Ruth");
Magazine.MagCards.Add("M. Piazza");
Magazine.MagCards.Add("J. Robinson");
AddCardsToMyDeck(Magazine.MagCards);

//Only Ruth and Piazza are added

我認為你正在倒退一部分。 你的問題的文字說你想要將卡片從雜志中添加到你的套牌中,如果卡片還沒有放在你的套牌中,但是代碼會查看你牌組中的卡牌,如果他們不在雜志中,他們會嘗試做一些事情。 無論哪種方式,你都是通過編寫for循環來做到這一點:

private void AddCardsToMyDeck(List<string> MagCards)
{
    var newCards = MagCards.Except(MyDeck.MyCards.Select(c => c.getCardName()));
    MyDeck.MyCards.AddRange(newCards.Select(c => new Card(c)).ToArray());
}

我不確定我理解你的問題是什么,但是為了解決你提出的問題,你可以使用linq將列表首先轉換為名稱列表。

  foreach (string crd in from c in MyDeck.MyCards select c.getCardName() )
  {
     if ( MagCards.Contains( crd ) )
     {
        break;
     }
     else
     {
        Card cd = new Card(MagCard);
        MyDeck.MyCards.Add(cd);
     }
  }

作為一個設計問題,Joel Coehoorn為此提出了一個很好的答案。

// get card names in your deck
var deckCardNames = MyDeck.Cards.Select( c => c.getCardName() );
// get card names in magazine cards that are not in your deck
var missingMagCardNames = MagCards.Except( deckCardNames );
// instantiate card objects for each missing card name
var missingCards = missingMagCardNames.Select(mc => new Card(mc));
// add missing cards to your deck
MyDeck.Cards.AddRange( missingCards );

// whole thing inline
MyDeck.Cards.AddRange( MagCards.Except( MyDeck.Cards.Select( c => c.getCardName() ) ).Select (mc => new Card(mc) ) );

c# 列表<object>列出<string> ()<div id="text_translate"><p> <strong>解決方案</strong>我已經簡單地解決了這個問題</p><pre> var list = (List<object>)response.Result; List<List<object>> listOfListOfObjects = list.OfType<List<object>>().ToList(); for (var i = 0; i < listOfListOfObjects.Count; i++) { liste.Add((listOfListOfObjects[i])[2].ToString()); //[2] returns to me href, [1] returns innertext etc.. }</pre><p> 謝謝大家!</p><p> <strong>問題</strong></p><p>我從帶有列表 object 的腳本中獲取結果,但我想通過將其放入字符串列表並使用 IndexOf 搜索關鍵字等來分析結果。我嘗試使用</p><pre>var strings = (from o in objects select o.ToString()).ToList();</pre><p> 但它不起作用,它將項目轉換為名稱為“System.Collections.Generic.List`1[System.Object]”,它不會轉換確切的項目。</p><p> 感謝@amaitland,這是我的 cefsharp 腳本代碼</p><pre>const string script = @"(function() { var linksArray = new Array(); for (var i = 0; i < document.links.length; i++) { linksArray[i] = [String(document.links[i].innerHTML), String(document.links[i].innerText), String(document.links[i].href)]; } return linksArray; })();"; browser.EvaluateScriptAsync(script).ContinueWith(x => { var response = x.Result; if (response.Success && response.Result.= null) { var list = (List<object>)response;Result; // I want to put that "list" into listBox</pre><p> 也是腳本的結果: <a href="https://i.stack.imgur.com/8nvYg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8nvYg.jpg" alt="腳本輸出"></a> <a href="https://i.stack.imgur.com/iN64t.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iN64t.jpg" alt="當我轉換為字符串時"></a></p></div></string></object>

[英]c# List<object> to List<string>()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 C#序列化 - 列表 <object> 包含字符串和List <objects> 在C#中將字符串數組分成兩個不同的字符串列表 我的問題是關於比較列表 <string> 帶字典 <string, List<string> &gt;使用C#Linq 使用C#LINQ連接已過濾的List <object> 列表 <string> C#列表 <string[]> 列表 <object[]> 轉變 c# 列表<object>列出<string> ()<div id="text_translate"><p> <strong>解決方案</strong>我已經簡單地解決了這個問題</p><pre> var list = (List<object>)response.Result; List<List<object>> listOfListOfObjects = list.OfType<List<object>>().ToList(); for (var i = 0; i < listOfListOfObjects.Count; i++) { liste.Add((listOfListOfObjects[i])[2].ToString()); //[2] returns to me href, [1] returns innertext etc.. }</pre><p> 謝謝大家!</p><p> <strong>問題</strong></p><p>我從帶有列表 object 的腳本中獲取結果,但我想通過將其放入字符串列表並使用 IndexOf 搜索關鍵字等來分析結果。我嘗試使用</p><pre>var strings = (from o in objects select o.ToString()).ToList();</pre><p> 但它不起作用,它將項目轉換為名稱為“System.Collections.Generic.List`1[System.Object]”,它不會轉換確切的項目。</p><p> 感謝@amaitland,這是我的 cefsharp 腳本代碼</p><pre>const string script = @"(function() { var linksArray = new Array(); for (var i = 0; i < document.links.length; i++) { linksArray[i] = [String(document.links[i].innerHTML), String(document.links[i].innerText), String(document.links[i].href)]; } return linksArray; })();"; browser.EvaluateScriptAsync(script).ContinueWith(x => { var response = x.Result; if (response.Success && response.Result.= null) { var list = (List<object>)response;Result; // I want to put that "list" into listBox</pre><p> 也是腳本的結果: <a href="https://i.stack.imgur.com/8nvYg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8nvYg.jpg" alt="腳本輸出"></a> <a href="https://i.stack.imgur.com/iN64t.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iN64t.jpg" alt="當我轉換為字符串時"></a></p></div></string></object> LinQ C#-根據對象列表中包含的字符串對對象列表進行分組 <string> 屬性 在C#的字符串列表中搜索混雜的字符串 在 c# 的字符串列表中查找字符串 C#字符串操作以生成字符串列表
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM