Bug in 'foreach to LINQ' conversion

The refactoring result from foreach to LINQ sequence is not valid:
using VS 2010 RC, ReSharper 5 v1619, .NET 4.0 Client profile
Original code

           foreach (string str in Tags)
            {
                string value = DataParser.FindInHeader(doc.Header, str);
                if (value == null) continue;

                DataTagProperty property = DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-", ""), value);
                property.OriginDate = doc.OriginDate;
                properties.Add(property);
            }



after partial LINQ conversion:

          foreach (DataTagProperty property in Tags
                .Select(str => new {str, value = DataParser.FindInHeader(doc.Header, str)})
                .Where(@t => value != null)
                .Select(@t => DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-", ""), value))) {
                property.OriginDate = doc.OriginDate;
                properties.Add(property);
            }
note on the second line, there should be (t => t.value != null)
also on the third line should be (t.str.Replace(



I've found out, that this problem occurs only in the first use. If i Undo this change and then again use 'convert to method chain', the result is valid.

Also a question, is it possible to convert foreach directly to method chain instead of LINQ sequence? Is there some kind of switch for this? Or is there a switch for wrapping extension method sequences the same way it's used in the second example?
2 comments
Comment actions Permalink

Hello Martin,

Do you get such code after "Convert part of body into LINQ-expression" or
after "Convert LINQ to methods chain"?

Valentin Kipiatkov
CTO and Chief Scientist
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

The refactoring result from foreach to LINQ sequence is not valid:
using VS 2010 RC, ReSharper 5 v1619, .NET 4.0 Client profile
Original code
foreach (string str in Tags)
{
string value = DataParser.FindInHeader(doc.Header,
str);
if (value == null) continue;
DataTagProperty property =
DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-",
""), value);
property.OriginDate = doc.OriginDate;
properties.Add(property);
}
after partial LINQ conversion:
foreach (DataTagProperty property in Tags
.Select(str => new {str, value =
DataParser.FindInHeader(doc.Header, str)})
.Where(@t => value != null)
.Select(@t =>
DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-",
""), value))) {
property.OriginDate = doc.OriginDate;
properties.Add(property);
}
note on the second line, there should be (t => t.value != null)
also on the third line should be (t.str.Replace(

I've found out, that this problem occurs only in the first use. If i
Undo this change and then again use 'convert to method chain', the
result is valid.

Also a question, is it possible to convert foreach directly to method
chain instead of LINQ sequence? Is there some kind of switch for this?
Or is there a switch for wrapping extension method sequences the same
way it's used in the second example?

---
Original message URL:
http://www.jetbrains.net/devnet/message/5257002#5257002



0
Comment actions Permalink

I use 'convert part of body to LINQ expression', the result is:

foreach (DataTagProperty property in from str in Tags let value = DataParser.FindInHeader(doc.Header, str) where value != null select DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-", ""), value)) {
                property.OriginDate = doc.OriginDate;
                properties.Add(property);
            }



no problem here; then I use 'Convert LINQ to method chain' and I get

foreach (DataTagProperty property in Tags.Select(str => new {str, value = DataParser.FindInHeader(doc.Header, str)}).Where(@t => value != null).Select(@t => DataPropertyGenerator.CreateDataTagProperty(str.Replace("X-DBWorld-", ""), value))) {
                property.OriginDate = doc.OriginDate;
                properties.Add(property);
            }

- broken result... but if I Undo this and do the same again, it's OK. Strange to me

0

Please sign in to leave a comment.