开发者

A better way than nesting foreach?

开发者 https://www.devze.com 2022-12-19 02:29 出处:网络
I\'ve got a list of Radios that I\'m trying to produce a more user-friendly version of.Behind the scenes all descriptions are stringIds and all part numbers are DB Ids.

I've got a list of Radios that I'm trying to produce a more user-friendly version of. Behind the scenes all descriptions are stringIds and all part numbers are DB Ids.

Right now, I've got this code:

       var z = (from w in wireless
                                         join s in allStrings on w.DescriptionId equals s.StringId
                                         join u in allUids on w.Uid equals u.Uid
                                         where s.LanguageId 开发者_如何转开发== 0
                                         select new {w, s, u});

        List<RadioProperty> rp = new List<RadioProperty>();
        foreach (var x in z)
        {
            foreach(var y in x.w.RadioToVoltage)
            {
                rp.Add(new RadioProperty
                {
                    PartNumber = x.u.PartNumber,
                    Description = x.s.Description,
                    CurrentType = y.Id.VoltageType,
                    Voltage = y.Id.VoltageValue,
                });
            }
        }

Basically, every radio part number can have several voltage options. We could have Radio ABC with 48VAC, 110VAC and 240VAC options, so I'm trying to create 3 separate RadioProperty items, one for each voltage option.

Is there a better way of doing this? I'm running .Net 3.5.

Thanks


not tested but you should be able to get it all in one linq statement somethin glike

 List<RadioProperty> rp = (from w in wireless
                     join s in allStrings on w.DescriptionId equals s.StringId
                     join u in allUids on w.Uid equals u.Uid
                     where s.LanguageId == 0
                     from t in w.RadioToVoltage
                     select new RadioProperty {
                         PartNumber = w.u.PartNumber,
                         Description = w.s.Description,
                         CurrentType = t.Id.VoltageType,
                         Voltage = t.Id.VoltageValue,
                     }).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消