C# Linq OrderBy [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 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);

Related

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"))}

What is the meaning of the mentioned query? [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
If I had the table "CustomerDetails" than what is the below query explains??
var details = (from data in entity.CustomerDetails where (data.CustomerId == CustId && data.CustomerProjectID == CustProjId) select data).FirstOrDefault();
Share the exact meaning. Thanks in advance.
That query selects the data from the CustomerDetails table where the CustomerId equals the given CustId and the CustomerProjectID equals the given CustProjId. It then returns the first element from the set that returns.
I personally find it easier to use LINQ Expressions like so:
var details = entity.CustomerDetails
.FirstOrDefault(cust =>
cust.CustomerId == CustId &&
cust.CustomerProjectID == CustProjId
);

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();

How to compare a date is in current month or not using c# [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
Sorry, for my question. Actually i am not able to clarify my question for my hurry up.
I have a table like below :
This table is LoginHistoryTable(When a user login my application, his history insert this table according to UserId). I get all data from table as a list.
Now i want to Know : How many times they logged in this month and the last month.
This is really simple but here you go. You need to compare month and year.
boo isCurrentMonth = DateTime.Now.Month == otherDate.Month
&& DateTime.Now.Year == otherDate.Year;
Something like..
public bool IsThisMonth(DateTime dt)
{
return dt.Month == DateTime.Now.Month;
}

Categories