0

BeerNames2番目のリストに名前がないリストからリストを取得したい。

var myList=(from f in Beers where ...
              select f.BeerNames).ToList

var storeList(from f in Store where...
                select f).ToList()

MyListには、StoreListにないBeerNameがいくつか含まれます。それらのBeerNameを見つけるにはどうすればよいですか?試し.Exceptてみ!Containsましたが、何か間違ったことをしていて、重要な知識が不足していることに気付きました。ありがとう

変えたら

var storeList(from f in Store where...
                select f).ToList()

var storeList(from f in Store where...
                select f.BeerNames).ToList()

その後、などを除いて使用できますList1.Except(List2)。もっと良い方法があるかどうかわかりませんでした。これが明確でない場合は申し訳ありませんが...私はしようとしています:)

4

2 に答える 2

2
var list1 = new String[] {"ABC1","ABC2", "ABC3", "ABC4"} ;
var list2 = new String[] {"ABC3","ABC4", "ABC5", "ABC6"} ;
var list3 = list1.Except(list2); // list3 holds ABC1, ABC2

正常に動作することを除いて。

linqクエリから返されたアイテムに問題があると思われます。最初のf.BeerNamesとStoreListのfが同じデータ型を指していないようです。

異種タイプの場合

var list1 = from s in new String[] {"ABC1","ABC2", "ABC3", "ABC4"} select new {BeerName=s,Id = Guid.NewGuid().ToString()}  ;
var list2 = new String[] {"ABC3","ABC4", "ABC5", "ABC6"} ;
var intermediateList = list1.Select(i=>i.BeerName).Except(list2);
var list3 = from l1 in list1
        join l2 in intermediateList on l1.BeerName equals l2
        select l1;


list1.Dump(); // run in linqPad
intermediateList.Dump();// run in linqPad
list3.Dump();// run in linqPad

list3は次を返します

BeerName Id

ABC1 569ace9a-66c4-46aa-bbf5-50d586a2886f

ABC2 af456094-9692-4771-b489-8b3cca8aa938

LinqPadを使用して上記を実行するか、.Dump()を削除してVSで実行します。

于 2012-05-08T14:51:20.290 に答える
1

文字列型で動作することを除いて

var myList = MyFrig.Select(f => f.BeerNames).Except(Store).ToList();
于 2012-05-08T14:50:40.977 に答える