Linq to SQL查询列数据中包含空格的列

时间:2012-05-16 18:15:56

标签: c# asp.net sql-server linq

使用Linq to SQL

如何查询和匹配包含来自辅助表的所有行以及列中包含空格的列?

表1:

ID  FAQ    MODELS 
1   faq1   model1 
2   faq2   model2 model1
3   faq3   model3 model2 model1   (Spaces in models column)

表2:

ID  MODELS PIC
1   model1 model1pic
2   model2 model2pic
3   model3 modal3pic

期待:

faq1 model1 model1pic
faq2 model1 model1pic
faq2 model2 model2pic
faq3 model1 model1pic
faq3 model2 model2pic
faq3 model3 model3pic


SELECT kwfaqtmp.faqmodelnum, kwFAQtmp.issue,
kwfaqtmp.resolution, kwtable4tmp.modelnum,     
kwtable4tmp.prodpic 
FROM kwfaqtmp AS t1 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
INNER JOIN dbo.kwtable4tmp 
AS t2 ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS = t2.modelnum 
COLLATE SQL_Latin1_General_CP1_CI_AS 
ORDER BY t1.issue;

3 个答案:

答案 0 :(得分:2)

如果您无法修复设计(您不应该在一个列中存储多个数据,如其他地方所指出的那样),您可以使用拆分功能完成此操作:

CREATE FUNCTION dbo.SplitStrings
(
  @List  NVARCHAR(MAX),
  @Delim NCHAR(1)
)
RETURNS TABLE
AS
   RETURN ( SELECT DISTINCT Item FROM
       ( SELECT Item = x.i.value('(./text())[1]', 'nvarchar(max)')
         FROM ( SELECT [XML] = CONVERT(XML, '<i>'
         + REPLACE(@List, @Delim, '</i><i>') + '</i>').query('.')
           ) AS a CROSS APPLY [XML].nodes('i') AS x(i) ) AS y
       WHERE Item IS NOT NULL
   );
GO

现在你可以说:

SELECT t1.FAQ, t2.MODELS, t2.PIC
FROM dbo.Table1 AS t1
CROSS APPLY dbo.SplitStrings(t1.MODELS, ' ') AS s
INNER JOIN dbo.Table2 AS t2
ON     s.Item COLLATE SQL_Latin1_General_CP1_CI_AS 
  = t2.Models COLLATE SQL_Latin1_General_CP1_CI_AS
ORDER BY t1.FAQ;

结果:

FAQ   MODELS  PIC
----  ------  ---------
faq1  model1  model1pic
faq2  model1  model1pic
faq2  model2  model2pic
faq3  model1  model1pic
faq3  model2  model2pic
faq3  model3  modal3pic

您尝试使用的查询:

 SELECT kwfaqtmp.faqmodelnum, kwFAQtmp.issue,kwfaqtmp.resolution, 
    kwtable4tmp.modelnum, kwtable4tmp.prodpic 
 FROM kwfaqtmp AS t1 
 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
 INNER JOIN dbo.kwtable4tmp AS t2 
   ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS 
     = t2.modelnum COLLATE SQL_Latin1_General_CP1_CI_AS 
 ORDER BY t1.issue;

根本无效。虽然它不应该产生你在评论中引用的确切错误。如我原始查询所示保持t1 / t2别名怎么样?

 SELECT t1.faqmodelnum, t1.issue, t1.resolution, 
    t2.modelnum, t2.prodpic 
 FROM kwfaqtmp AS t1 
 CROSS APPLY dbo.SplitStrings(t1.faqmodelnum, ' ') AS s 
 INNER JOIN dbo.kwtable4tmp AS t2 
   ON s.item COLLATE SQL_Latin1_General_CP1_CI_AS 
     = t2.modelnum COLLATE SQL_Latin1_General_CP1_CI_AS 
 ORDER BY t1.issue;

答案 1 :(得分:0)

您应该在映射类中使用ColumnAttribute。 创建一个POCO类,其字段名不带空格,映射到表中带空格的字段。

答案 2 :(得分:0)

我认为您需要重构数据库。

FAQ (remove MODELS column)

ID  FAQ
1   faq1
2   faq2
3   faq3

FAQMODELS (Create this table)
ID FAQ   MODELS
1  faq1  model1
2  faq2  model2
3  faq2  model1
4  faq3  model3
5  faq3  model2
6  faq3  model1

MODELSPIC    
ID  MODELS PIC
1   model1 model1pic
2   model2 model2pic
3   model3 modal3pic

然后你可以

SELECT
    FAQ, 
    MODELS, 
    PIC 
FROM FAQMODELS INNER JOIN MODELSPIC ON FAQMODELS.MODELS = MODELSPIC.MODELS