Order by in Linq when object is null

Dinesh Bali
Dinesh Bali used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Need to check for null.Using C# 6.0 syntax your code looks like:
var titleElements = xml.Elements(elementName)
					.OrderBy( e =>  e.Element("SortOrder")?.Value)
					.OrderBy( e =>  e.Element("Name").Value)
					.ToList();

Open in new window

Author

Commented:
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.

Author

Commented:
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

Author

Commented:
Many Thanks Your solution worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial