according to your comment, the problem is where u try to create a string
from BoatName, BoatType and BoatOverAllLength
, you cant format a string in linq to entities
, as i said before (prev post, no3), you can fetch data you need and then select
in memory to create BoatDets
string, so this, surely works:
var slipDets6 = (from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new {s6, jn})
.ToList()
.Select(u => new
{
SlipID = (int?)u.s6.SlipId,
SlipNumber = u.s6.SlipNumber,
Length = u.s6.Length,
Electricity = u.s6.Electricity,
Telephone = u.s6.Telephone,
TV = u.s6.TV,
BoatDets = u.jn == null ? "" : u.jn.BoatName + " [" + u.jn.BoatType + ", " + u.jn.BoatOverAllLength + "]",
Status = u.s6.Status
})
.ToList();
or, you can fetch BoatName, BoatType and BoatOverAllLength
as property, and when your query fetched, create string you need from that properties, something like this:
public class FlatSlip
{
public int? SlipID { get; set; }
public string SlipNumber { get; set; }
public string Length { get; set; }
public string Electricity { get; set; }
public string Telephone { get; set; }
public string TV { get; set; }
public string BoatName { get; set; }
public string BoatType { get; set; }
public string BoatOverAllLength { get; set; }
public string Status { get; set; }
public string BoatDets
{
get
{
return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
}
}
}
var slipDets6 = from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new FlatSlip()
{
SlipID = (int?)s6.SlipId,
SlipNumber = s6.SlipNumber,
Length = s6.Length,
Electricity = s6.Electricity,
Telephone = s6.Telephone,
TV = s6.TV,
BoatName = jn == null ? "" : jn.BoatName,
BoatType = jn == null ? "" : jn.BoatType,
BoatOverAllLength = jn == null ? "" : jn.BoatOverAllLength,
Status = s6.Status
};
or if you are insisting on using literal
:
public class Boat
{
public Boat()
{
}
public Boat(string BoatName, string BoatType, string BoatOverAllLength)
{
this.BoatName = BoatName;
this.BoatType = BoatType;
this.BoatOverAllLength = BoatOverAllLength;
}
public string BoatName { get; set; }
public string BoatType { get; set; }
public string BoatOverAllLength { get; set; }
public string BoatDets
{
get
{
return this.BoatName + " [" + this.BoatType + ", " + this.BoatOverAllLength + "]";
}
}
}
var slipDets6 = (from s6 in db.Slips
join b6 in db.Boats on s6.BoatId equals b6.BoatId into temp
from jn in temp.DefaultIfEmpty()
orderby s6.SlipNumber
select new { s6, jn })
.ToList()
.Select(u => new
{
SlipID = (int?)u.s6.SlipId,
SlipNumber = u.s6.SlipNumber,
Length = u.s6.Length,
Electricity = u.s6.Electricity,
Telephone = u.s6.Telephone,
TV = u.s6.TV,
BoatDets = jn == null ? new Boat() : new Boat(u.jn.BoatName, u.jn.BoatType, u.jn.BoatOverAllLength),
Status = u.s6.Status
})
.ToList();
notice: BoatDets
property, in my two last queries, can not be used in linq to entity
, and its readable when your data has been fetched to memory