63

I have below code in c# 4.0.

//Dictionary object with Key as string and Value as List of Component type object
Dictionary<String, List<Component>> dic = new Dictionary<String, List<Component>>();

//Here I am trying to do the loping for List<Component>
foreach (List<Component> lstComp in dic.Values.ToList())
{
    // Below I am trying to get first component from the lstComp object.
    // Can we achieve same thing using LINQ?
    // Which one will give more performance as well as good object handling?
    Component depCountry = lstComp[0].ComponentValue("Dep");
}

WCF tries to distance itself from ASP.NET, because it can run in any number of hosts - not just ASP.NET. This also allows it to cut out large chunks of the ASP.NET pipe, improving throughput. You can force it to use ASP.NET mode by adding:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

to the configuration, but frankly you're better off (IMO) using the thread principal instead. WCF has full support for the principal model.

4

10 に答える 10

109

試す:

var firstElement = lstComp.First();

アイテムが入っていないFirstOrDefault()場合にも使用できます。lstComp

http://msdn.microsoft.com/en-gb/library/bb340482(v=vs.100).aspx

編集:

を取得するにはComponent Value:

var firstElement = lstComp.First().ComponentValue("Dep");

これは、 に要素があると仮定しlstCompます。別のより安全な方法は...

var firstOrDefault = lstComp.FirstOrDefault();
if (firstOrDefault != null) 
{
    var firstComponentValue = firstOrDefault.ComponentValue("Dep");
}
于 2013-04-23T08:13:27.977 に答える
2

できるよ

Component depCountry = lstComp
                       .Select(x => x.ComponentValue("Dep"))
                       .FirstOrDefault();

または、値の辞書全体に対してこれが必要な場合は、キーに結び付けることもできます

var newDictionary = dic.Select(x => new 
            {
               Key = x.Key,
               Value = x.Value.Select( y => 
                      {
                          depCountry = y.ComponentValue("Dep")
                      }).FirstOrDefault()
             }
             .Where(x => x.Value != null)
             .ToDictionary(x => x.Key, x => x.Value());

これにより、新しい辞書が作成されます。値にアクセスできます

var myTest = newDictionary[key1].depCountry     
于 2013-04-23T08:58:47.080 に答える
1

次のように使用できるlinq式の場合:

 List<int> list = new List<int>() {1,2,3 };
        var result = (from l in list
                     select l).FirstOrDefault();

このように使用できるラムダ式の場合

リスト list = new List() { 1, 2, 3 }; int x = list.FirstOrDefault();

于 2013-04-23T08:47:24.807 に答える
1

これを試して、最初にすべてのリストを取得してから、目的の要素を取得します(あなたの場合は最初と言ってください):

var desiredElementCompoundValueList = new List<YourType>();
dic.Values.ToList().ForEach( elem => 
{
   desiredElementCompoundValue.Add(elem.ComponentValue("Dep"));
});
var x = desiredElementCompoundValueList.FirstOrDefault();

多くの foreach 反復と変数割り当てなしで最初の要素の値を直接取得するには、次のようにします。

var desiredCompoundValue = dic.Values.ToList().Select( elem => elem.CompoundValue("Dep")).FirstOrDefault();

2 つのアプローチの違いを確認してください。最初のアプローチでは、ForEach を介してリストを取得し、次に要素を取得します。2 番目の方法では、値を直接取得できます。

同じ結果、異なる計算;)

于 2013-04-23T08:30:38.397 に答える
1

これを使用することもできます:

var firstOrDefault = lstComp.FirstOrDefault();
if(firstOrDefault != null) 
{
    //doSmth
}
于 2013-04-23T08:15:46.160 に答える
0

私は次のようにします:

//Dictionary object with Key as string and Value as List of Component type object
Dictionary<String, List<Component>> dic = new Dictionary<String, List<Component>>();

//from each element of the dictionary select first component if any
IEnumerable<Component> components = dic.Where(kvp => kvp.Value.Any()).Select(kvp => (kvp.Value.First() as Component).ComponentValue("Dep"));

ただし、リストにComponentクラスまたは子のオブジェクトのみが含まれていることが確実な場合のみ

于 2013-04-23T08:39:10.380 に答える
0

そのような方法はたくさんあります:
.First .FirstOrDefault .Single .SingleOrDefault
最も適したものを選択してください。

于 2013-04-23T08:14:33.573 に答える