Link to home
Start Free TrialLog in
Avatar of Dinesh Bali
Dinesh Bali

asked on

Order by in Linq when object is null

Hi,

I want to do order by in linq
if SortOrder is null then sorder by name in asecding order
If SortOrder is there then order by SortOrder.

Please advise how to set order by in my below query.
In below Query I am setting error at el.Element("SortOrder").Value as null object.

Error Below:
System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Xml.Linq.XContainer.Element(...) returned null.


even getting above error in
el.Element("Name").Value

XElement xml = XElement.Load(Server.MapPath(ConstantKeys.CountryCurrencyFilePath));
            IEnumerable<XElement> titleElements = from el in xml.Elements(elementName)
                                                  orderby el.Element("SortOrder").Value ascending, el.Element("Name").Value ascending
                                                  select el;

Open in new window



Sample XML below. You can see SortOrder element is available for AUD and AZN. But it is not available for others.
Please advise us on the query.

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Row>
    <Code>AFN</Code>
    <Name>Afghani</Name>
  </Row>
  <Row>
    <Code>AOA</Code>
    <Name>Kwanza</Name>
  </Row>
  <Row>
    <Code>ARS</Code>
    <Name>Argentine Peso</Name>
  </Row>
  <Row>
    <Code>AUD</Code>
    <Name>Australian Dollar</Name>
    <SortOrder>1</SortOrder>
  </Row>
  <Row>
    <Code>AZM</Code>
    <Name>Azerbaijan Manat</Name>
  </Row>
  <Row>
    <Code>AZN</Code>
    <Name>New Azerbaijan Manat</Name>
    <SortOrder>2</SortOrder>
  </Row>
</Root>

Open in new window


Regards,
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dinesh Bali
Dinesh Bali

ASKER

Many Thanks.

I applied the code as advised. The code is giving error at line:

.OrderBy(e => e.Element("Name").Value)

Code below:
var titleElements = xml.Elements(elementName)
                    .OrderBy(e => e.Element("SortOrder")?.Value)
                    .OrderBy(e => e.Element("Name").Value)
                    .ToList();

Open in new window


Error below:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Xml.Linq.XContainer.Element(...) returned null.

Open in new window


But when I apply ? for Name element as below then my code works.
But order by SortOrder is not happening. The list is as per Name only.

But I wanted the prference should be  SortOrder . If sortorder is not available then order should be Name

So, order by should first do order by
SortOrder and then Name.

Therefore Australian Dollar should come on first then Afghani

<Row>
    <Code>AUD</Code>
    <Name>Australian Dollar</Name>
    <SortOrder>1</SortOrder>
  </Row>


var titleElements = xml.Elements(elementName)
                    .OrderBy(e => e.Element("SortOrder")?.Value)
                    .OrderBy(e => e.Element("Name")?.Value)
                    .ToList();

Open in new window


Please advise.
I just realized at the end of method before return
Ordering is already done as below

 var ddlist = titleList.OrderBy(a => a.CurrencyName);
            return ddlist.ToList<UserCountryDetailsBE>();

Open in new window


I wanted to add a.SortOrder as well first before it orders a.CurrencyName). Please advise.

 
var ddlist = titleList.OrderBy(a => a.SortOrder and a.CurrencyName);
            return ddlist.ToList<UserCountryDetailsBE>();

Open in new window

Many Thanks Your solution worked.