foreach (var item in itemList)
{
var nameValueList =
item.Attributes.Select(
attribute => new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))).
ToList();
var nameValuePackage = new NameValuePackage(new DataValue(item.Name), nameValueList);
nameValuePackageList.Add(nameValuePackage);
}
Сделал решарпер:
return (from item in itemList
let nameValueList = item.Attributes.Select(attribute => new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))).ToList()
select new NameValuePackage(new DataValue(item.Name), nameValueList)).ToList();
Скажите поклав руку на сердце: улучшилась ли читабельность кода?
0K>foreach (var item in itemList)
0K> {
0K> var nameValueList =
0K> item.Attributes.Select(
0K> attribute => new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))).
0K> ToList();
0K> var nameValuePackage = new NameValuePackage(new DataValue(item.Name), nameValueList);
0K> nameValuePackageList.Add(nameValuePackage);
0K> }
0K>
0K>Сделал решарпер:
0K>
0K>return (from item in itemList
0K> let nameValueList = item.Attributes.Select(attribute => new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))).ToList()
0K> select new NameValuePackage(new DataValue(item.Name), nameValueList)).ToList();
0K>
0K>Скажите поклав руку на сердце: улучшилась ли читабельность кода?
Как было что-то непонятное так и осталось, вот только Linq тут не при чем совершенно.
Здравствуйте, 0K, Вы писали:
0K>Скажите поклав руку на сердце: улучшилась ли читабельность кода?
Я бы сделал вот так:
var q = from item in itemList
let nameValues = from attribute in item.Attributes
select new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))
select new NameValuePackage(new DataValue(item.Name), nameValues.ToList())
return q.ToList();
Здравствуйте, Lloyd, Вы писали:
L>Я бы сделал вот так: L>
L>var q = from item in itemList
L> let nameValues = from attribute in item.Attributes
L> select new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value))
L> select new NameValuePackage(new DataValue(item.Name), nameValues.ToList())
L>return q.ToList();
L>
А я бы так:
var q = from item in itemList
let nameValues = item.Attributes.Select(a => new NameValue(new DataValue(a.Name), new DataValue(a.Value))
select new NameValuePackage(new DataValue(item.Name), nameValues.ToList())
return q.ToList();
К топикстартеру: по мне так читабельность заметно выросла по сравнению с вариантом совсем без linq:
foreach (var item in itemList)
{
var nameValueList = new List<NameValue>();
foreach (var attribute in item.Attributes)
{
nameValueList.Add(new NameValue(new DataValue(attribute.Name), new DataValue(attribute.Value)));
}
var nameValuePackage = new NameValuePackage(new DataValue(item.Name), nameValueList);
nameValuePackageList.Add(nameValuePackage);
}
Z>var q = from item in itemList
Z> let nameValues = item.Attributes.Select(a => new NameValue(new DataValue(a.Name), new DataValue(a.Value))
Z> select new NameValuePackage(new DataValue(item.Name), nameValues.ToList())
Z>return q.ToList();
Z>
Забил бы тогда уж совсем на query comprehension, чего уж там.