Click here to Skip to main content
15,885,853 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
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:

C#
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:

SQL
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#.

SQL
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

Larry Freedman
Posted
Updated 15-Jul-16 7:22am
v4

Looks like you're using LINQ to Entities? So, what's happening is that the LINQ internals are trying to convert string.Format back to something on SQL Server. Which...it can't.

You have two options:
1) Go the class readonly property route and drop the concat in the select.
2) Convert the collection to some kind of enumerable first.


So, here's #1:
public class pmDues
{
    [Key]
    public int DuesID { get; set; }
    [DataMember]
    public string dues_type { get; set; }
    public decimal dues_amount { get; set; }
    public string DuesText
    {
        get
        {
            return string.Format("{0} - {1}", dues_type, dues_amount);
        }
    }
}


Because you're selecting into the new objects first, the properties should be populated, thus allowing the concat to work. I can't 100% verify this right now, but I'm pretty sure it will work.

Number 2 feels a little more hackish to me. You'll need to do something like in the following [post].


Cheers,
-James
 
Share this answer
 
If all you're doing is looking to coerce that string for display purposes (and don't care about preserving the text in dues_text for other purposes, try this out:

public IQueryable GetDues()
{
    return from dues in ObjectContext.tblDuesRates 
           orderby dues.due_type
           let dueText = string.Format("{0} - {1}", dues.due_type, dues.amount.Value)
           select new pmDues
              {
                DuesID = dues.id,
                dues_type = dueText,
                dues_amount = dues.amount.Value 
              };
}


All we're doing is concating with string.Format. The let allows an inline value to be constructed. This also helps it to be a little more readable.

Also, how is it not working? Are you getting an error? It might be that dues.amount.Value is null, which would throw an exception.

Alternatively, if you are looking to create something that is always accessible and you want to preserve the value of dues_type, create a read-only property on your class that does the same string.Format to return the other two properties.

Cheers,
-James
 
Share this answer
 
v3
Comments
larry118 12-May-10 12:42pm    
Thank you for responding but unfortunately it did not work. The DomainService liked the code but when I try to run the application I get the following message:

System.Service.Model.DomainService.Client.DomainOperationException Load operation failed for Query 'GetDues'.Linq to Entities does not recognize the method System.String.Format(System.String.System.Object, System.Object, System.Object)' method, and this method cannot be transalted into a store expression

Could I be missing a reference?

Larry
Thanks I just added {1:F2} and that's got it!

I appreciate your help.

Larry
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900