Using .ToString on a Linq? [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 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();

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

Linq ForEach in string interpolation [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 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"))}

How to linq to split string (only if delimer exists) and create array [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 5 years ago.
Improve this question
public class itemObject { public string items; }
values - item = "name1,name2" or items = "item3"
Need linq to split by ',' if exists else one string array.
This doesnt require linq, its the default behaviour of String.Split
var array = items.Split(',');

C# Linq OrderBy [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 am trying to sort my customers by their last order by using linq.
_list = _data.OrderBy(adress => adress.LastOrder.Date);
My Problem here is, that for some customers the LastOrder is NULL
How can I solve this problem?
Try this:
_list = _data.OrderBy(adress =>
adress.LastOrder == null
? DateTime.MaxValue
: adress.LastOrder.Date);
You need to choose between DateTime.MaxValue and DateTime.MinValue depending on how you want to order null items.
You have to handle the null case:
var orderedByLastOrder = _data
.OrderBy(x => x.LastOrder == null ? DateTime.MinValue : x.LastOrder.Date);

Categories