-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 回答