繁体   English   中英

在LINQ C#中修剪字符串

[英]Trim string in LINQ C#

我想修剪下面的字符串,但有一个错误:

var getClients = (from c in GeneralUtillities)
    orderby c.Client_Name
    select new
    {
        c.Client_Name.Trim(),
        c.Client_Code,
    });

日Thnx

您需要为Anonymous类型对象属性提供名称

var getClients = (from c in GeneralUtillities)
    orderby c.Client_Name
    select new
    {
        Name = c.Client_Name.Trim(),
        Client_Code = c.Client_Code
    };

如果未在匿名类型属性中提供名称,它将尝试使用其分配的值的属性名称。 当您在属性上调用方法时,它无法解析名称。 你需要指定它:

var getClients = (from c in GeneralUtillities)
orderby c.Client_Name
select new
{
    Client_Name = c.Client_Name.Trim(),
    c.Client_Code,
});

我看到3件事 - 因为你没有指明错误我不确定真正的问题是什么,但这里有一些猜测:

  1. 你在年底得到了一个闭合括号GeneralUtillities这是一个语法错误
  2. 您没有为匿名类型中的第一个字段指定名称
  3. Linq-to-Entities可能不支持使用Trim

这是另一种选择:

var getClients = (from c in GeneralUtillities
    orderby c.Client_Name)
    .AsEnumerable()
    .Select (c =>  new
        {
            Client_Name = c.Client_Name.Trim(),
            Client_Code = c.Client_Code,  // for readability, not necessary
        });

必须在编译时知道匿名类型的属性名称。

var getClients = (from c in GeneralUtillities)
    orderby c.Client_Name
    select new
    {
        Name= c.Client_Name.Trim(),
        Code = c.Client_Code,
    });
var getClients =
    (from c in GeneralUtillities.a.data
    orderby c.Client_Name
    select new
    {
        c.ID_Client,
        c.Client_Name,
    });

这是正确的代码,因此问题是修剪客户端名称在开始和结束时没有空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM