【问题标题】:How to Concat String in LINQ SQL WHERE clause?如何在 LINQ SQL WHERE 子句中连接字符串?
【发布时间】:2016-03-22 01:05:59
【问题描述】:

我想在linq中连接where,但我在网上找不到答案,所以在这个网站上提问。这是我的问题。

在mysql中:

select * from table where concat(field1, ' ', field2) = 'field1 字段2';

如何在 linq sql 中转换?

【问题讨论】:

    标签: sql-server linq


    【解决方案1】:

    您应该能够通过以下方式轻松完成此操作(我使用的是 NorthWind 数据库和流利的 Linq-to-SQL):

    Employees.Where (e => e.FirstName + " " + e.LastName == "Nancy Davolio")
    

    【讨论】:

    • 南希·达沃里奥是谁?
    • 你的问题让我发笑——据我所知,Nancy Davolio 不是一个真实的人,但她已经在 Northwind 示例数据库中呆了很长时间。我笑了,因为她的名字对我来说几乎就像是某种计算机术语。与 Albert Futerkist 相同……原因相同。
    • 我虽然她是你的情人。谢谢你的琐事。我学到了一些新东西而不是我的问题。
    【解决方案2】:
    string x = "field1 field2";
    
    List<Tuple<string, string>> tupleList = new List<Tuple<string, string>>();
    tupleList.Add(new Tuple<string, string>("field1", "field2"));
    tupleList.Add(new Tuple<string, string>("field1", "field3"));
    tupleList.Add(new Tuple<string, string>("field1", "field4"));
    tupleList.Add(new Tuple<string, string>("field1", "field5"));
    tupleList.Add(new Tuple<string, string>("field1", "field6"));
    tupleList.Add(new Tuple<string, string>("field1", "field2"));
    
    var results = tupleList.Where(l => String.Concat(l.Item1, " ", l.Item2) == x);
    
    results.Dump();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2021-06-21
      • 2012-01-21
      • 1970-01-01
      相关资源
      最近更新 更多