Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends,

a new question in text changing with linq.

I have a long string that can be splitted ino an array of lines
I want to update only somelines in the array with a specific text and after update join all the lines to 1 string again.

I know how to update line by line and call the method recursive untill no more occurrences are found. But something tells me I could adjust all these lines in 1 linq call.

now written:
C#
if (completeText.Contains("exception for sqlstate value"))
            {
                string[] stringSeparators = new string[] { "\n" };
                string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None);

                var line = lines.Where(x=>x.Contains("exception for sqlstate value") && !x.Trim().StartsWith("--"));
                if (line.Count() > 0)
                {
                    string lineNew = "--" + line.First().Trim();
                    completeText = completeText.Replace(line.First().Trim(), lineNew);
                    if (line.Count() > 1)
                        completeText = CheckForSqlStateValue(completeText);
                }
            }
            return completeText;


How to change in 1 call without recursive methods calls?
Posted
Comments
Nathan Minier 1-Oct-15 7:38am    
The only issue I'm seeing is the multiple enumerations. You could just assign var first = lines.First() and work from there.

Have you tried something like:

var line = lines.Where(x=>x.Contains("exception for sqlstate value") && !x.Trim().StartsWith("--")).First().Select(x => { x = "--" + x.Trim() })
Herman<T>.Instance 1-Oct-15 7:56am    
I understand your idea, but I want:
lines = lines.Where(x => x....) with a replace funcionality.
Nathan Minier 1-Oct-15 8:09am    
There's a tiny extension method on SO that would work:
http://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet

Nice debate about it as well.
Herman<T>.Instance 1-Oct-15 8:38am    
Tried ForEacth without succes
Maciej Los 1-Oct-15 8:26am    
var lines = completeText.Split(stringSeparators, StringSplitOptions.None).Where(x=>x.Contains("exception for sqlstate value") && !x.Trim().StartsWith("--")); ???

1 solution

If i understand you well... you want to replace "exception for sqlstate value" with "int =" if a line does not start with "--".

If you want to return all lines with changed values, try this:
C#
string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None)
		.Select(item=>item.Contains("a") && !item.Trim().StartsWith("--") ? item.Replace("a", "int=") : item)
		.ToArray();


Complete - tested - example:
C#
string completeText = "a\nb\nc\n--a\ne\nf\na\nh\ni\n--a\nk\nl";
string[] stringSeparators = new string[] { "\n" };
string[] lines = completeText.Split(stringSeparators, StringSplitOptions.None)
        .Select(item=>item.Contains("a") && !item.Trim().StartsWith("--") ? item.Replace("a", "int=") : item)
        .ToArray();

Returns an array:
int=
b
c
--a
e
f
int=
h
i
--a
k
l


But...
If you want to return lines which meet your criteria, use Where + Select
 
Share this answer
 
Comments
Herman<T>.Instance 1-Oct-15 9:46am    
*|* making a deep bow and waving the hat to you *|*
Maciej Los 1-Oct-15 10:23am    
:shamefaced:
Thank you.
Herman<T>.Instance 1-Oct-15 10:29am    
I have more fancier LINQ questions for ya :oh no: like:
sql => select @is_allowed = case when(access&0x08) > 0 1 else 0 end
should be => select @is_allowed = case when(access&0x08) > 0 then 1 else 0 end

How to?
Maciej Los 1-Oct-15 11:01am    
You mean you want to return true/false with linq query?
Herman<T>.Instance 1-Oct-15 11:03am    
Got that fixed.
Create an array split on space. insert in array to list at position of else -1. Works like a charm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900