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

4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to get related table names using linq in mvc3.The code i posted works well,but wat chnges i have to make to work it in mvc3
Make the views part of the linq-to-sql context.
got an error when i post your code in Linqer "Error Compiling Expression: Too many characters in character literal "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.