Linq ForEach in string interpolation [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In order to make my code superclear, I'm trying to do something like that:
var result = $#"...
<div class='modal-body'>
{content}
</div>
{(haveButtons ? "" : "<div class='modal-footer'>")}
{modalButtons.ForEach(m => "INSERT SOME HTML")}
{(haveButtons ? "" : "</div>")}
..."
But, of course, that doesn't compile because in the lambda I need to put some code and not just a magic return. Is there any way to do that?

{string.Join("", modalButtons.Select(m => "INSERT SOME HTML"))}

Related

C# Get Object Values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to C#.
I have a method that returns an object with the List of objects inside.
0 {{DapperRow, RecordedFormID = 'id1'}} object {Dapper.SqlMapper.DapperRow}
1 {{DapperRow, RecordedFormID = 'id2'}} object {Dapper.SqlMapper.DapperRow}
How can I get those ids as strings?
Here is a screenshot
You can use System.Linq namespace and do this:
yourObject.Select(x => x.RecordedFormID);

How to split and then join comma separated words into a string to create condition? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have given words like:
general, sports, weather
I want to create a push notification condition like:
condition = "'general' in topics || 'sports' in topics || 'weather' in topics",
How can I do this in one -or two- shot in C# without looping? So I can build a new string and I use it like below:
condition = dynamicCondition,
Try this one:
string input = "general, sports, weather";
string output = string.Join(" || ", input.Split(',').Select(s => $"'{s}' in topics"));
Note: don't forget to using System.Linq

Remove quotes from the start and end of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the string
"{ "type" : "Stage_FF_Hot_Alerts__c"}"
I want to it be
{ "type" : "Stage_FF_Hot_Alerts__c"}
i.e. I want to remove quotes from start and end. How can I achieve this?
You could use Trim:
var a = "\"{ \"type\" : \"Stage_FF_Hot_Alerts__c\"}\"";
var b = a.Trim('"');
Console.WriteLine(a);
Console.WriteLine(b);
It will remove all leading and trailing quotation marks.
Try it online
You can also use:
var quotedString = "\"hello\"";
var unQuotedString = quotedString.TrimStart('"').TrimEnd('"');

How do I add text into my string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I though it was simple. but I didn't manage to make this line of code add "\Game_Progress.xml" to my String
code_file.Insert(code_file.Length, "\\Game_Progress.xml");
You can append the string using the += operator:
code_file += "\\Game_Progress.xml";
Note: If you want to combine a path, you should consider using the Path Combine method:
System.IO.Path.Combine(code_file, "\\Game_Progress.xml")
C# can require an #prior to the string when using backslashes. Try:
code_file.Insert(code_file.Length, #"\\Game_Progress.xml");

Using .ToString on a Linq? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I do a link who have a value on the where that I have to transform into a string
Like this example:
Fund.ObjectName = context.CR_TASK.First(a => Convert.ToString(a.TASK_NO) == item.DATA_TEXT).TASK_TITLE;
I would use FirstOrDefault to assign null if no item matches:
Fund.ObjectName = context.CR_TASK
.Where(a => a.TASK_NO.ToString() == item.DATA_TEXT)
.Select(a => a.TASK_TITLE)
.FirstOrDefault();

Categories