在Linq查询中连接字符串和小数 [英] Concatenate a string and decimal in a Linq query

查看:101
本文介绍了在Linq查询中连接字符串和小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS2010,Framework 4.0,C#和silverlight 4中编写应用程序。



我有一个简单的类和Linq查询。



课程是:



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; }
    }





在我的DomainService中,我有以下IQueryable:





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
              };
        }



到目前为止很好.......但是!



什么我真的想要显示连接的dues_type + dues_amount。 dues_amount在SQL Server表中声明为十进制。我不知道的是在哪里以及如何将dues_type与dues_amount连接起来。我试过以下但是没有用。我应该在课堂上连接吗?如果是这样的话。仍然是C#的新手。




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
              };
        }







谢谢



Larry Freedman




Thanks

Larry Freedman

推荐答案

看起来你正在使用LINQ to Entities?所以,正在发生的事情是LINQ内部正在尝试将string.Format转换回SQL Server上的内容。哪个......它不能。



你有两种选择:

1)去类readonly属性路线并删除co​​ncat在选择中。

2)首先将集合转换为某种可枚举。





所以,这里是#1:

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);
        }
    }
}





因为你首先选择新对象,应该填充属性,从而允许concat工作。我现在无法100%验证这一点,但我很确定它会起作用。



2号对我来说感觉更加狡猾。您需要执行以下操作[发布]。





干杯,

-James



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


如果你正在做的就是为了显示目的而强迫该字符串(并且不关心在dues_text中保留文本用于其他目的,请尝试这样做:



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 
              };
}





全部我们正在做的是用字符串连接。格式。 let允许构造内联值。这也有助于它更具可读性。



此外,它是如何工作的?你收到错误了吗?可能是dues.amount.Value为null,这会抛出异常。



或者,如果你想创建一些总是可访问的东西,你想要的东西为了保留dues_type的值,在你的类上创建一个只读属性来执行相同的string.Format以返回其他两个属性。



干杯,

-James



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


谢谢我刚刚添加了{1:F2}就可以了!



我感激你的帮助。



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

I appreciate your help.

Larry


这篇关于在Linq查询中连接字符串和小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆