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").Va lue as null object.
Error Below:
System.NullReferenceExcept ion: '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
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.
Regards,
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").Va
Error Below:
System.NullReferenceExcept
System.Xml.Linq.XContainer
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;
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>
Regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just realized at the end of method before return
Ordering is already done as below
I wanted to add a.SortOrder as well first before it orders a.CurrencyName). Please advise.
Ordering is already done as below
var ddlist = titleList.OrderBy(a => a.CurrencyName);
return ddlist.ToList<UserCountryDetailsBE>();
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>();
ASKER
Many Thanks Your solution worked.
ASKER
I applied the code as advised. The code is giving error at line:
.OrderBy(e => e.Element("Name").Value)
Code below:
Open in new window
Error below:
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
Open in new window
Please advise.