Reputation: 7149
I am writing an application in VS2010, Framework 4.0, C# and silverlight 4.
I have a simple class and Linq Query.
The class is:
public class pmDues
{
[Key]
public int DuesID { get; set; }
[DataMember]
public string dues_type { get; set; }
public decimal dues_amount { get; set; }
}
In my DomainService I have the following IQueryable:
public IQueryable<pmdues> GetDues()
{
return from dues in ObjectContext.tblDuesRates
orderby dues.due_type
select new pmDues()
{
DuesID = dues.id,
dues_type = dues.due_type,
dues_amount = dues.amount.Value
};
}
So far so good.......But!
What I really want is to display the dues_type + dues_amount concatenated. The dues_amount is declared as a decimal in the SQL Server table. What I don't know is where and how to concatenate dues_type with dues_amount. I tried the following but it did not work. Should I do the concatenation in the class? If so how. Still kind of new to C#.
public IQueryable<pmdues> GetDues()
{
return from dues in ObjectContext.tblDuesRates
orderby dues.due_type
select new pmDues()
{
DuesID = dues.id,
dues_type = dues.due_type + " - " +
dues.amount.Value.ToString(),
dues_amount = dues.amount.Value
};
}
Thanks
Upvotes: 0
Views: 2815
Reputation: 106640
Use SqlFunctions.StringConvert
, which is found in System.Data.Entity.SqlServer
in EF6 (System.Data.Objects.SqlClient
in previous versions) in order to convert the decimal
to a string
:
dues_type = dues.due_type + " - " + SqlFunctions.StringConvert(dues.amount),
Upvotes: 2
Reputation: 1968
There is SqlFunctions class, which has utility methods, treated in special way, while converting expression tree to sql query and that's why invoked at db level, but it's available only for Entity Framework > 4 (if I'm not mistaken).
But you could concatenate your values at .NET side. Just use AsEnumerable
for this kind of operations.
public IEnumerable<pmdues> GetDues()
{
var pmDues =
from dues in ObjectContext.tblDuesRates
orderby dues.due_type
select new
{
DuesID = dues.id,
dues_type = dues.due_type,
dues_amount = dues.amount.Value
};
return pmDues
.AsEnumerable()
.Select(d =>
new pmDues()
{
DuesID = d.DuesID,
DuesType = d.dues_type + " - " +
d.dues_amount.ToString()
});
}
But you won't be able to return IQueryable
and thus perform filtering or sorting at db level then.
If upper level code, which invokes GetDues
, really should perform such operations,
you can provide additional parameter of type Func<IQueryable<pmdues>, IQueryable<pmdues>>
to your method and invoke it before AsEnumerable
.
Upvotes: 1
Reputation: 13676
What is your problem here ? You can always use string concatenation with either way: String.Concat(a, b, c.ToString())
or just a + b + c.ToString()
and write this value to your string ' dues_type
' . What's wrong with this ?
Upvotes: 0
Reputation: 13248
Making an assumption here but give this a go and see if it works for you.
public IQueryable<pmdues> GetDues()
{
return (from dues in ObjectContext.tblDuesRates
orderby dues.due_type).ToList().
Select(x => new pmDues
{
DuesID = x.id,
dues_type = string.Format("{0} - {1}",
x.due_type,
x.amount),
dues_amount = x.amount.Value
}).AsQueryable();
}
Upvotes: 0
Reputation: 4963
Try with removing .ToString()
public IQueryable<pmdues> GetDues()
return from dues in ObjectContext.tblDuesRates
orderby dues.due_type
select new pmDues()
{
DuesID = dues.id,
dues_type = dues.due_type + " - " +
dues.amount.Value,
dues_amount = dues.amount.Value
};
}
Upvotes: 0