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,
Dinesh BaliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dinesh BaliAuthor 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.
0
Dinesh BaliAuthor 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

0
Dinesh BaliAuthor Commented:
Many Thanks Your solution worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.