enter code hereIn my SQL Server database I have my address information for the subNumber (e.g. Unit 802) and the streetNumber (e.g. 242 Elizabeth Street) stored separately.
I need to display these as one (i.e. 802/242 Elizabeth Street) if the subNumber contains a value, otherwise just return the streetNumber if it does not.
I’ve been working toward a solution using IF ELSE and a foreach loop after accessing the data through LINQ – but I’m stuck after the point where I have completed the loop. I would also be happy to do this with a SELECT Stored Procedure in SQL – open to suggestions!
DataClassesDataContext dc = new DataClassesDataContext();
var recent = from p in dc.Properties
orderby p.modtime descending
where p.status == "Current"
select new
{
rsub = (p.subNumber).ToString(),
rnumber = (p.streetNumber).ToString(),
rstreet = p.street,
rsuburb = p.suburb,
rurl = p.propertyURL,
};
foreach (var home in recent)
{
if (string.IsNullOrEmpty(home.rsub))
{
string rnum = home.rnumber;
}
else
{
string rnum = home.rsub + "/" + home.rnumber;
}
}
recentrepeater.DataSource = recent;
recentrepeater.DataBind();
Yahia gave the best option in c# – this is the SQL solution I have finally ended up with:
ALTER PROCEDURE GetPropertyShort
AS
SELECT TOP 5 ISNULL(convert(varchar(5), subNumber) + '/' + convert(varchar(5), streetNumber), convert(varchar(5), streetNumber)) as Number, street, suburb, propertyURL, modtime
FROM Property
ORDER BY modtime DESC
try