1

This is my SQL query

SELECT 
sys.sysobjects.name Name,
sys.foreign_keys.*
FROM 
sys.foreign_keys 
inner join sys.sysobjects on
    sys.foreign_keys.parent_object_id = sys.sysobjects.id
WHERE 
referenced_object_id = OBJECT_ID(N'[dbo].[Country]') 

I have installed Linqer to convert SQL to linq. But I got an error:

SQL cannot be converted to LINQ: Table [foreign_keys] not found in the current Data Context.

I am a beginner in Linq. Can Anyone help me to convert SQL to Linq

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Mizbella
  • 936
  • 2
  • 15
  • 30
  • http://stackoverflow.com/questions/296972/sql-to-linq-tool and http://stackoverflow.com/questions/1339732/convert-sql-to-linq-to-sql and http://stackoverflow.com/questions/12238423/linqpad-convert-sql-to-linq-command – imonas May 22 '13 at 07:21
  • imonas:I alrweady have Linqer and linqpad.But am getting error 'SQL cannot be converted to LINQ: Table [foreign_keys] not found in the current Data Context.' – Mizbella May 22 '13 at 07:26
  • The problem is that system views will not be picked up by Linqer. If you want to read these tables in your application, first create your own views on them, as was done [here](http://stackoverflow.com/a/8099311/861716) and write a query on these views. – Gert Arnold May 22 '13 at 08:40
  • @GertArnold :-Can u please give me little more explanation... – Mizbella May 22 '13 at 09:06

1 Answers1

1

The problem is that system views will not be picked up by Linqer. If you want to read these tables in your application, first create your own views on them, as was done here and write a query on these views.

CREATE VIEW SysObjectsView AS SELECT * FROM sys.sysobjects;
GO
CREATE VIEW SysForeignKeysView AS SELECT * FROM sys.foreign_keys;
GO

SELECT obj.name Name, fk.*
FROM SysForeignKeysView fk
INNER JOIN SysObjectsView obj ON fk.parent_object_id = obj.id
INNER JOIN SysObjectsView objfk ON fk.referenced_object_id = objfk.id
WHERE objfk.name = N'Country' 

Linqer should be able to pick up these views.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291