Can I use a TryParse inside Linq Comparable? - c#

A sort of:
Documenti = Documenti
.OrderBy(o => string.IsNullOrEmpty(o.Note))
.ThenBy(o => Int32.TryParse(o.Note))
.ToList();
That will "ignore" (not order, putting at the end) if o.Note is "" or not an int.
How can I do it?

Everyone who uses C#7 or newer scroll to the bottom, everyone else can read the original answer:
Yes, you can, if you pass the correct parameters to int.TryParse. Both overloads take the int as out-parameter and initialize it inside with the parsed value. So like this:
int note;
Documenti = Documenti
.OrderBy(o => string.IsNullOrEmpty(o.Note))
.ThenBy(o => Int32.TryParse(o.Note, out note))
.ToList();
The clean approach is using a method that parses to int and returns int? if unparseable:
public static int? TryGetInt(this string item)
{
int i;
bool success = int.TryParse(item, out i);
return success ? (int?)i : (int?)null;
}
Now you can use this query(OrderByDescending because true is "greater" than false):
Documenti = Documenti.OrderByDescending(d => d.Note.TryGetInt().HasValue).ToList();
It's cleaner than using a local variable that is used in int.TryParse as out parameter.
Eric Lippert commented another answer of me where he gives an example when it might hurt:
C# LINQ: How is string("[1, 2, 3]") parsed as an array?
Update, this has changed with C#7. Now you can declare the variable directly where you use out parameters:
Documenti = Documenti
.OrderBy(o => string.IsNullOrEmpty(o.Note))
.ThenBy(o => Int32.TryParse(o.Note, out int note))
.ToList();

Documenti = Documenti.OrderBy(o =>
int.TryParse(o.Note, out int val)
? val
: int.MaxValue /* or int.MinValue */
).ToList();
Note: Toggling between int.MaxValue and int.MinValue will either put the empty values at the front or the end of the list.
EDIT: 2020-02-07 Using an inline out variable which was introduced in C# 7

You can actually put much more complex logic in the lambda expression:
List<Doc> Documenti = new List<Doc>() {
new Doc(""),
new Doc("1"),
new Doc("-4"),
new Doc(null) };
Documenti = Documenti.OrderBy(o => string.IsNullOrEmpty(o.Note)).ThenBy(o =>
{
int result;
if (Int32.TryParse(o.Note, out result))
{
return result;
} else {
return Int32.MaxValue;
}
}).ToList();
foreach (var item in Documenti)
{
Console.WriteLine(item.Note ?? "null");
// Order returned: -4, 1, <empty string>, null
}
Remember, o => Int32.TryParse(...) is just a shorthand for creating a delegate that just takes in o as a parameter and returns Int32.TryParse(...). You can make it do whatever you want as long as it still is a syntacticly correct method with the correct signature (ex, all code paths return an int)

That won't produce the expected results b/c TryParse returns a bool rather than int. The easiest thing to do is create a function that returns an int.
private int parseNote(string note)
{
int num;
if (!Int32.TryParse(note, out num))
{
num = int.MaxValue; // or int.MinValue - however it should show up in sort
}
return num;
}
call that function from your sort
Documenti = Documenti
.OrderBy(o => parseNote(o.Note))
.ToList();
you could do it inline too, but, i think a separate method makes the code more readable. i'm sure the compiler will inline it, if it's an optimization.

C# 7 has some new features that make this even easier
var ints = from a in str.Split(',').Select(s=> new { valid = int.TryParse(s, out int i), result = i })
where a.valid
select a.result;
or as you are asking specifically about sorting
var ints = from a in str.Split(',')
orderby (int.TryParse(s, out int i) ? i : 0 )
select a.result;

Related

An expression tree may not contain an out argument variable declaration

I am using c# 7.3 with new feature to create a generic method where the type should be an enum.
I have a method like this:
public static bool TryConvertToEnum<T>(this int value, out T returnedValue)
where T : struct, Enum
{
if (Enum.IsDefined(typeof(T), value))
{
returnedValue = (T)Enum.ToObject(typeof(T), value);
return true;
}
returnedValue = default;
return false;
}
It will try to convert an int to a specific enum. I am trying to use this method in two cases. One does work while the other case doesn't.
Here is the working example:
if (documentTypeId.TryConvertToEnum(out DocumentType returnedValue)
&& returnedValue == DocumentType.Folder)
{
//In this case it works fine
}
If I try to use this in a select method it does not work:
var comments = await DatabaseService.GetAll(filter)
.OrderByDescending(x => x.Id)
.ToPaginated(page)
.Select(x => new PostCommentViewModel
{
Id = x.Id,
Status = x.Status.TryConvertToEnum(out PostCommentStatusType returnedValue) ?
returnedValue : PostCommentStatusType.None //Here it does not work
}).ToListAsync();
In the second case it does not allow the project to be build. It gives the error:
An expression tree may not contain an out argument
variable declaration
When I hover, RSharper does show a popup stating: Expression tree may not contain an out argument variable declaration
I am little confused to the part may, not sure if expression tree can have or not out params...
Does anybody have any idea why this happens ?
It seemed to be an easy fix actually. I only had to materialize data before applying select function :(palmhand).
var comments = DatabaseService.GetAll(filter)
.OrderByDescending(x => x.Id)
.ToPaginated(page)
.ToList()//Applied ToList here
.Select(x => new PostCommentViewModel
{
Id = x.Id,
Comment = x.Comment,
Created = x.Created,
Name = x.Name,
ParentId = x.ParentId,
PostId = x.PostId,
Status = x.Status.TryConvertToEnum(out PostCommentStatusType returnedValue) ?
returnedValue : PostCommentStatusType.None
}).ToList();

compare none value by linq

I have an integer column(not null) in a sql server 2008 database/table, I want to retrieve it.
// go to the table and get the largest number, if none exists, use 0.
int iNumber = iContext.DetailsRecords.Max(x => x.Number); // from entity
But at the very beginning, the table is empty. I want to set is as 0.
How to change the code?
If you don't want to check if the DetailsRecords is null, but rather handle if the source sequence is empty, then use this answer (I adjusted it a bit differently for your LINQ usage):
Max or Default?
int iNumber = iContext.DetailsRecords.Select(x => (int?)x.Number).Max() ?? 0;
Enumerable.Max Method (IEnumerable<Int32>): InvalidOperationException -
source contains no elements.
Enumerable.Max Method (IEnumerable<Nullable<Int32>>): If the source sequence is empty or contains only values that are null, this function returns null.
You can use DefaultIfEmpty for this. If the sequence is empty it will return the provided item as the only item in the sequence, if not it will return the original sequence.
IEnumerable<int> numbers = new int [] { };
numbers.DefaultIfEmpty(0).Max();
try this:
int iNumber = iContext.DetailsRecords==null? iContext.DetailsRecords.Max(x => x.Number) : 0;
or
int iNumber = iContext.DetailsRecords.Any()? iContext.DetailsRecords.Max(x => x.Number) : 0; if table is not null and contains 0 records.
Use the Any function to see if there are any records:
int iNumber = iContext.DetailsRecords.Any()
? iContext.DetailsRecords.Max(x => x.Number)
: 0;
I know this already has an accepted answer, but another good way would just be FirstOrDefault().
int iNumber = iContext.DetailsRecords.OrderByDescending(x => x.Number).FirstOrDefault();
I use this way often and also can let you setup a new object if the result was null.
MyObject m = mySearch.GetItems.Where(a => a.id == id).FirstOrDefault() ?? new MyObject();

Linq's FirstOrDefault to act as other type?

I have this code :
var res1 = dtData.AsEnumerable()
.Where(...)
.Select(f => new { val = f["PremiumAfterUWDiscount"].ToDecimalOrZero(),
idpolicy = f["IdPolicy"].ToString() })
.FirstOrDefault();
however , since this returns an anonymous type , its default value is null .
I want it to act as FirstOrDefault for int type.
so if there is no record , it will return 0 ( default behavior as int).
is it possible ?
p.s. ( of course i can check it in a condition but still , i prefer the linq way).
Return an anonymous type that signifies "nothing" and either use the null coalescing operator:
var res1 = dtData.AsEnumerable()
.Where(...)
.Select(f => new { val = f["PremiumAfterUWDiscount"].ToDecimalOrZero(),
idpolicy = f["IdPolicy"].ToString() })
.FirstOrDefault() ?? new { val = 0, idpolicy = "" };
Or the DefaultIfEmpty extension method:
var res1 = dtData.AsEnumerable()
.Where(...)
.Select(f => new { val = f["PremiumAfterUWDiscount"].ToDecimalOrZero(),
idpolicy = f["IdPolicy"].ToString() })
.DefaultIfEmpty(new { val = 0, idpolicy = "" })
.FirstOrDefault();
You would only be able to return an int in place of an anonymous type if you in fact return an object and cast later on (as per #recursive's answer), but this to me seems counter-productive.
FirstOrDefault does not offer a way to specify what "default" is.
You can't have an expression evaluate to 2 different data types on two execution paths.
Also, even if that's possible, var is different to dynamic so the variable type won't be inferred by the compiler.
Doing this doesn't make any sense, and I would encourage you to think about why you want to do this, and find some cleaner, more direct way to accomplish it.
With that said, here's a small tweak to #IronicMuffin's approach that will actually work.
object res1 = dtData.AsEnumerable()
.Where(...)
.Select(f => new {
val = f["PremiumAfterUWDiscount"].ToDecimalOrZero(),
idpolicy = f["IdPolicy"].ToString()
})
.FirstOrDefault() as object ?? 0;
I don't think this is very useful though.

Getting a cell from DataTable.Row.ItemArray with Linq

I have the following ItemArray:
dt.Rows[0].ItemArray.. //{0,1,2,3,4,5}
the headers are : item0,item1,item2 etc..
So far, to get a value from the ItemArray I used to call it by an index.
Is there any way to get the value within the ItemArray with a Linq expression based on the column name?
Thanks
You can also use the column-name to get the field value:
int item1 = row.Field<int>("Item1");
DataRow.Item Property(String)
DataRow.Field Method: Provides strongly-typed access
You could also use LINQ-to-DataSet:
int[] allItems = (from row in dt.AsEnumerable()
select row.Field<int>("Item1")).ToArray();
or in method syntax:
int[] allItems = dt.AsEnumerable().Select(r => r.Field<int>("Item1")).ToArray();
If you use the Item indexer rather than ItemArray, you can access items by column name, regardless of whether you use LINQ or not.
dt.Rows[0]["Column Name"]
Tim Schmelter's answer is probably what you are lookin for, just to add also this way using Convert class instead of DataRow.Field:
var q = (from row in dataTable.AsEnumerable() select Convert.ToInt16(row["COLUMN1"])).ToArray();
Here's what I've come up with today solving a similar problem. In my case:
(1)I needed to xtract the values from columns named Item1, Item2, ... of bool type.
(2) I needed to xtract the ordinal number of that ItemN that had a true value.
var itemValues = dataTable.Select().Select(
r => r.ItemArray.Where((c, i) =>
dataTable.Columns[i].ColumnName.StartsWith("Item") && c is bool)
.Select((v, i) => new { Index = i + 1, Value = v.ToString().ToBoolean() }))
.ToList();
if (itemValues.Any())
{
//int[] of indices for true values
var trueIndexArray = itemValues.First().Where(v => v.Value == true)
.Select(v => v.Index).ToArray();
}
forgot an essential part: I have a .ToBoolean() helper extension method to parse object values:
public static bool ToBoolean(this string s)
{
if (bool.TryParse(s, out bool result))
{
return result;
}
return false;
}

LINQ query null exception when Where returns 0 rows

I have the following LINQ method that works as expected except if there are No Rows Found then I get a Null Exception. I am struggling on how I modify this to return 0 if that occurs.
public static int GetLastInvoiceNumber(int empNumber)
{
using (var context = new CmoDataContext(Settings.Default.LaCrosse_CMOConnectionString))
{
context.Log = Console.Out;
IQueryable<tblGreenSheet> tGreenSheet = context.GetTable<tblGreenSheet>();
return (tGreenSheet
.Where(gs => gs.InvoiceNumber.Substring(2, 4) == empNumber.ToString())
.DefaultIfEmpty()
.Max(gs => Convert.ToInt32(gs.InvoiceNumber.Substring(6, gs.InvoiceNumber.Length)))
);
}
}
Thanks
I tried one of Jon Skeet's suggestions, below, and now I get Unsupported overload used for query operator 'DefaultIfEmpty'
public static int GetLastInvoiceNumber(int empNumber)
{
using (var context = new CmoDataContext(Settings.Default.LaCrosse_CMOConnectionString))
{
context.Log = Console.Out;
IQueryable<tblGreenSheet> tGreenSheet = context.GetTable<tblGreenSheet>();
return tGreenSheet
.Where(gs => gs.InvoiceNumber.Substring(2, 4) == empNumber.ToString())
.Select(gs => Convert.ToInt32(gs.InvoiceNumber.Substring(6, gs.InvoiceNumber.Length)))
.DefaultIfEmpty(0)
.Max();
}
}
You're using
.Where(...)
.DefaultIfEmpty()
which means if there are no results, pretend it's a sequence with a single null result. You're then trying to use that null result in the Max call...
You can probably change it to:
return tGreenSheet.Where(gs => ...)
.Max(gs => (int?) Convert.ToInt32(...)) ?? 0;
This uses the overload finding the maximum of int? values - and it returns an int? null if there were no values. The ?? 0 then converts that null value to 0. At least, that's the LINQ to Objects behaviour... you'll have to check whether it gives the same result for you.
Of course, you don't need to use the ?? 0 if you're happy to change the method signature to return int? instead. That would give extra information, in that the caller could then tell the difference between "no data" and "some data with a maximum value of 0":
return tGreenSheet.Where(gs => ...)
.Max(gs => (int?) Convert.ToInt32(...));
Another option is to use the overload of DefaultIfEmpty() which takes a value - like this:
return tGreenSheet.Where(gs => ...)
.Select(gs => Convert.ToInt32(...))
.DefaultIfEmpty(0)
.Max();
In situations like this when there may or may not be a matching item, I prefer to return an object rather than a value type. If you return a value type, you have to have some semantics about what value means "there is nothing here." I would change it to return the last invoice, then (when it is non-null) get the invoice number from the invoice. Add a method to the class to return the numeric invoice number from the string.
public static tbleGreenSheet GetLastInvoice(int empNumber)
{
using (var context = new CmoDataContext(Settings.Default.LaCrosse_CMOConnectionString))
{
context.Log = Console.Out;
return context.GetTable<tblGreenSheet>()
.Where(gs => gs.InvoiceNumber.Substring(2, 4) == empNumber.ToString())
.OrderByDescending(gs => Convert.ToInt32(gs.InvoiceNumber.Substring(6, gs.InvoiceNumber.Length)))
.FirstOrDefault();
}
}
public class tbleGreenSheet
{
....
public int NumericInvoice
{
get { return Convert.ToInt32(InvoiceNumber.Substring(6, InvoiceNumber.Length)); }
}
...
}
Used as
var invoice = Foo.GetLastInvoice( 32 );
if (invoice != null)
{
var invoiceNumber = invoice.NumericInvoice;
...do something...
}
else
{
...do something else...
}
I had a remarkably similar experience with IQueryable<T> and NHibernate. My solution:
public static TExpr MaxOrDefault<TItem, TExpr>(this IQueryable<TItem> query,
Expression<Func<TItem, TExpr>> expression) {
return query.OrderByDescending(expression).Select(expression).FirstOrDefault();
}
The only drawback is that you are stuck with the standard default value, instead of getting to specify one.

Categories