简体   繁体   English

Linq-to-sql查询,其翻译是从逗号分隔的代码到描述的逗号分隔

[英]Linq-to-sql query with translation from comma-separated of code to comma-separated of description

I've the following tables: 我有以下表格:

  1. "procedures": “程序”:

    code: bigint, primary key, auto-increment 代码:bigint,主键,自动递增

    caption: varchar(max), not-null 标题:varchar(max),非空

  2. "worklist": “工作清单”:

    code: bigint, primary key, auto-increment 代码:bigint,主键,自动递增

    title: varchar(max), not-null 标题:varchar(max),非空

    procedures: varchar(max), not-null, comma-separated string of procedure-code 过程:varchar(max),非空,过程代码的逗号分隔字符串

    ... ...

I'm using Linq-to-SQL to query the table "worklist" with column "procedures" to be translated to comma-separated string of procedure-caption. 我正在使用Linq-to-SQL查询带有“过程”列的表“工作列表”,该表将转换为以逗号分隔的过程标题字符串。

eg the sub-query for "worklist"."procedures": 例如,“工作列表”。“过程”的子查询:

procedures = string.Join(",", (
    from pc in w.procedures.Split(',').Select(cs => long.Parse(cs)).ToList()
    join ps in db.procedures.AsEnumerable() on pc equals ps.code
    select ps.caption
).ToArray()),

However, I'm facing the Exception of "Split() has no supported translation in SQL". 但是,我面临“ Split()在SQL中不支持翻译”的异常。

Please kindly advise how to do that. 请提出建议。 Many Thanks! 非常感谢!

you will not be able to solve the query as such. 您将无法像这样解决查询。 You need to fetch your raw data to c# and afterwards process the split. 您需要将原始数据获取到c#,然后处理拆分。

This is because in your current code you are asking sql server to do the split and it has no implementation for that. 这是因为在您当前的代码中,您正在要求sql server进行拆分,并且它没有实现。 Hence the error... 因此错误...

The following will pass in each item in procList as a separate sql parameter so that it can put it in a sql IN clause. 以下内容将procList中的每个项目作为单独的sql参数传入,以便可以将其放入sql IN子句中。 If there's not too many of them, this should work fine. 如果没有太多,这应该可以正常工作。

var procList = w.procedures.Split(',').Select(cs => long.Parse(cs)).ToList();

var procedures = (from ps in db.procedures.AsEnumerable()
    where procList.Contains(ps.Code)
    select ps.Caption).ToArray()

In the query you have shown, you will have to first fetch the records from db using linq query like: 在显示的查询中,您将必须首先使用linq查询从db获取记录,例如:

var abc = w.procedures.ToList();

or 要么

var abc = w.procedures.ToList().Select(cs => cs).Split(...);

now on abc you can use any of your string functions.. Because once you do ToList it will fetch the records from database. 现在在abc上,您可以使用任何字符串函数。.因为一旦执行ToList,它将从数据库中获取记录。

Hope this Helps.. 希望这可以帮助..

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

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