Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL LINQ VS2010 Server
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
 
Larry Freedman
Posted 12-May-10 5:38am
Edited 12-May-10 10:44am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi James,
 
It's a nice answer. Exactly same thing I was looking for & found it here.
 
Regards,
Kunal
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanks I just added {1:F2} and that's got it!
 
I appreciate your help.
 
Larry
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Comments
larry118 at 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

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



Advertise | Privacy | Mobile
Web04 | 2.8.150326.1 | Last Updated 12 May 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100