ArgumentException throws when sort a column with null value in the dynamic collection










4














I am trying to sort the dynamic collection and it is working fine when the column doesn't have null value. If the column having null value, then sorting the dynamic collection throws ArgumentException.Here is the sample dynamiccollectionsimplesample.zip .



`var dt = GetDataTable();
var dynamicCollection = new ObservableCollection<ExpandoObject>();
foreach (System.Data.DataRow row in dt.Rows)

ExpandoObject dyn = new ExpandoObject();
dynamicCollection.Add(dyn);
foreach (System.Data.DataColumn column in dt.Columns)

var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];


var expressionFunc = GetExpressionFunc("OrderID");
var source = dynamicCollection.AsQueryable();
var sourceType = source.ElementType;
var paramExpression = Expression.Parameter(sourceType, sourceType.Name);
var cExp = Expression.Constant("OrderID");
var iExp = Expression.Invoke(expressionFunc, new Expression cExp, paramExpression );
var lambda = Expression.Lambda(iExp, paramExpression);
var orderedSource = source.Provider.CreateQuery(
Expression.Call(typeof(Queryable),"OrderBy",new Type source.ElementType,lambda.Body.Type ,source.Expression,
lambda));
var enumerable = orderedSource as IEnumerable;
if (enumerable.GetEnumerator().MoveNext())

`


Could anyone please update me, why sorting with null value in dynamic collection throws exception?
For null values in the dynamic collection, null values are shown as Curly phrase like this shown in below image,
Value of the null value column










share|improve this question





















  • The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
    – Christopher
    Nov 12 at 10:06










  • A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
    – jdweng
    Nov 12 at 10:27










  • the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
    – jdweng
    Nov 12 at 10:39











  • It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
    – Gowtham Gopalsamy
    Nov 12 at 11:14















4














I am trying to sort the dynamic collection and it is working fine when the column doesn't have null value. If the column having null value, then sorting the dynamic collection throws ArgumentException.Here is the sample dynamiccollectionsimplesample.zip .



`var dt = GetDataTable();
var dynamicCollection = new ObservableCollection<ExpandoObject>();
foreach (System.Data.DataRow row in dt.Rows)

ExpandoObject dyn = new ExpandoObject();
dynamicCollection.Add(dyn);
foreach (System.Data.DataColumn column in dt.Columns)

var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];


var expressionFunc = GetExpressionFunc("OrderID");
var source = dynamicCollection.AsQueryable();
var sourceType = source.ElementType;
var paramExpression = Expression.Parameter(sourceType, sourceType.Name);
var cExp = Expression.Constant("OrderID");
var iExp = Expression.Invoke(expressionFunc, new Expression cExp, paramExpression );
var lambda = Expression.Lambda(iExp, paramExpression);
var orderedSource = source.Provider.CreateQuery(
Expression.Call(typeof(Queryable),"OrderBy",new Type source.ElementType,lambda.Body.Type ,source.Expression,
lambda));
var enumerable = orderedSource as IEnumerable;
if (enumerable.GetEnumerator().MoveNext())

`


Could anyone please update me, why sorting with null value in dynamic collection throws exception?
For null values in the dynamic collection, null values are shown as Curly phrase like this shown in below image,
Value of the null value column










share|improve this question





















  • The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
    – Christopher
    Nov 12 at 10:06










  • A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
    – jdweng
    Nov 12 at 10:27










  • the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
    – jdweng
    Nov 12 at 10:39











  • It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
    – Gowtham Gopalsamy
    Nov 12 at 11:14













4












4








4







I am trying to sort the dynamic collection and it is working fine when the column doesn't have null value. If the column having null value, then sorting the dynamic collection throws ArgumentException.Here is the sample dynamiccollectionsimplesample.zip .



`var dt = GetDataTable();
var dynamicCollection = new ObservableCollection<ExpandoObject>();
foreach (System.Data.DataRow row in dt.Rows)

ExpandoObject dyn = new ExpandoObject();
dynamicCollection.Add(dyn);
foreach (System.Data.DataColumn column in dt.Columns)

var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];


var expressionFunc = GetExpressionFunc("OrderID");
var source = dynamicCollection.AsQueryable();
var sourceType = source.ElementType;
var paramExpression = Expression.Parameter(sourceType, sourceType.Name);
var cExp = Expression.Constant("OrderID");
var iExp = Expression.Invoke(expressionFunc, new Expression cExp, paramExpression );
var lambda = Expression.Lambda(iExp, paramExpression);
var orderedSource = source.Provider.CreateQuery(
Expression.Call(typeof(Queryable),"OrderBy",new Type source.ElementType,lambda.Body.Type ,source.Expression,
lambda));
var enumerable = orderedSource as IEnumerable;
if (enumerable.GetEnumerator().MoveNext())

`


Could anyone please update me, why sorting with null value in dynamic collection throws exception?
For null values in the dynamic collection, null values are shown as Curly phrase like this shown in below image,
Value of the null value column










share|improve this question













I am trying to sort the dynamic collection and it is working fine when the column doesn't have null value. If the column having null value, then sorting the dynamic collection throws ArgumentException.Here is the sample dynamiccollectionsimplesample.zip .



`var dt = GetDataTable();
var dynamicCollection = new ObservableCollection<ExpandoObject>();
foreach (System.Data.DataRow row in dt.Rows)

ExpandoObject dyn = new ExpandoObject();
dynamicCollection.Add(dyn);
foreach (System.Data.DataColumn column in dt.Columns)

var dic = (IDictionary<string, object>)dyn;
dic[column.ColumnName] = row[column];


var expressionFunc = GetExpressionFunc("OrderID");
var source = dynamicCollection.AsQueryable();
var sourceType = source.ElementType;
var paramExpression = Expression.Parameter(sourceType, sourceType.Name);
var cExp = Expression.Constant("OrderID");
var iExp = Expression.Invoke(expressionFunc, new Expression cExp, paramExpression );
var lambda = Expression.Lambda(iExp, paramExpression);
var orderedSource = source.Provider.CreateQuery(
Expression.Call(typeof(Queryable),"OrderBy",new Type source.ElementType,lambda.Body.Type ,source.Expression,
lambda));
var enumerable = orderedSource as IEnumerable;
if (enumerable.GetEnumerator().MoveNext())

`


Could anyone please update me, why sorting with null value in dynamic collection throws exception?
For null values in the dynamic collection, null values are shown as Curly phrase like this shown in below image,
Value of the null value column







c# .net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 10:01









Gowtham Gopalsamy

211




211











  • The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
    – Christopher
    Nov 12 at 10:06










  • A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
    – jdweng
    Nov 12 at 10:27










  • the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
    – jdweng
    Nov 12 at 10:39











  • It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
    – Gowtham Gopalsamy
    Nov 12 at 11:14
















  • The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
    – Christopher
    Nov 12 at 10:06










  • A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
    – jdweng
    Nov 12 at 10:27










  • the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
    – jdweng
    Nov 12 at 10:39











  • It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
    – Gowtham Gopalsamy
    Nov 12 at 11:14















The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
– Christopher
Nov 12 at 10:06




The default sorter, is usually a Instance Function. And instance functions can not be called on null references. And rather then running into a "Object reference not set to a instance of object" exception, it throws a Argument one. You should try to provide a custom sorter, where you can filter out the null case.
– Christopher
Nov 12 at 10:06












A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
– jdweng
Nov 12 at 10:27




A Date cannot be null in c#. I will see if a can fix the posted code in teh zip file.
– jdweng
Nov 12 at 10:27












the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
– jdweng
Nov 12 at 10:39





the issue is with this line : collection.Rows.Add(null, "h", 978.76, 458.21, 675.99, null); The DateTime is null. Instead of null use : new DateTime(). This will set the date to 1/1/01. Then later you can ignore by adding a Where and filter for dates > 1/1/2000.
– jdweng
Nov 12 at 10:39













It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
– Gowtham Gopalsamy
Nov 12 at 11:14




It's our customer requirement . I set the numeric column and DateTime column as null value.Then, the ArgumentException is occurred.
– Gowtham Gopalsamy
Nov 12 at 11:14

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259773%2fargumentexception-throws-when-sort-a-column-with-null-value-in-the-dynamic-colle%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259773%2fargumentexception-throws-when-sort-a-column-with-null-value-in-the-dynamic-colle%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3