-3

私はLinqにかなり慣れていません。これをコーディングするのに問題があります。

多くの異なるサブリストを含むリストがあります。

oldList[0]    some type 
oldList[1]    another different type
oldList[2]    the type I want
oldList[3]    more types

特定のタイプからすべてのパラメーターを選択して、一時リストに書き込みたい。その一時リストが空の場合、いくつかの値を割り当てたいと思います(値は実際には重要ではありません)。値を変更した後、一時リストをoldListに書き戻したいと思います。

お知らせ下さい。これは私にとって大きな学習経験です。

public void myFunction(list)
    {
        //list contains at least 5 sublists of various type

        //check if the type I want is null
            IEnumerable<TypeIWant> possiblyEmptyList = list.OfType<TypeIWant>(); //find the type I want from the list and save it
            if (possiblyEmptyList == null) //this doesn't work and possiblyEmptyList.Count <= 1 doesn't work
            {
                //convert residence address to forwarding address
                IEnumerable<ReplacementType> replacementList = list.OfType<ReplacementType>();
                forwardingAddress = replacementList.Select(x => new TypeIWant /* this statement functions exactly the way I want it to */
                {
                    Address1 = x.Address1,
                    Address2 = x.Address2,
                    AddressType = x.AddressType,
                    City = x.City,
                    CountryId = x.CountryId,
                    CountyRegion = x.CountyRegion,
                    Email = x.Email,
                    ConfirmEmail = x.ConfirmEmail,
                    Fax = x.Fax,
                    Telephone = x.Telephone,
                    State = x.State,
                    PostalCode = x.PostalCode
                });
                //write forwarding address back to list
                //don't know how to do this
    }
4

2 に答える 2

2

LINQ の目的はクエリです。コレクション内の一部のアイテムを他のアイテムに置き換えることはできません。代わりに単純なループを使用してください:

IEnumerable<TypeIWant> possiblyEmptyList = list.OfType<TypeIWant>();
if (!possiblyEmptyList.Any()) 
{
   for (int i = 0; i < list.Count; i++)
   {
        ReplacementType item = list[i] as ReplacementType;
        if (item == null)
            continue;

        list[i] = ConvertToTypeIWant(item);
   }
}

そして変換 (これはautomapperのようなもので行う方が良いです):

private TypeIWant ConvertToTypeIWant(ReplacementType x)
{
     return new TypeIWant 
            {
                Address1 = x.Address1,
                Address2 = x.Address2,
                AddressType = x.AddressType,
                City = x.City,
                CountryId = x.CountryId,
                CountyRegion = x.CountyRegion,
                Email = x.Email,
                ConfirmEmail = x.ConfirmEmail,
                Fax = x.Fax,
                Telephone = x.Telephone,
                State = x.State,
                PostalCode = x.PostalCode
            };
}
于 2012-12-17T16:03:12.323 に答える
0

LINQ ではなく、例です。

class Program
{
    static void Main(string[] args)
    {
        // Vars
        var list = new List<List<string>>();
        var a = new List<string>();
        var b = new List<string>();
        var c = new List<string> { "one", "two", "three" };
        var d = new List<string>();

        // Add Lists
        list.Add(a);
        list.Add(b);
        list.Add(c);
        list.Add(d);

        // Loop through list
        foreach (var x in list)
        {
            if (x.Count < 1)
            {
                var tempList = new List<string>();

                tempList.Add("some value");

                x.Clear();
                x.AddRange(tempList);
            }
        }

        // Print
        int count = 0;
        foreach (var l in list)
        {
            count++;
            Console.Write("(List " + count + ") ");
            foreach (var s in l)
            {
                Console.Write(s + " ");
            }
            Console.WriteLine("");
        }

    }
}

(リスト 1) 何らかの値

(リスト 2) 何らかの値

(リスト 3) ワン ツー スリー

(リスト 4) 何らかの値

于 2012-12-17T16:05:41.437 に答える