Red Swan
Red Swan

Reputation: 15545

Can we use Linq with View of sql server?

As per my understanding Views could improve performance and I intend to use Views in my database and because my database is huge, I want to create the views for different queries. Is there any way to access the Views in .Net Windows applications or can we use LINQ with Views? What is the exact way to use View in .Net, so that i can improve performance? I am writing a C# desktop application.

Upvotes: 0

Views: 7478

Answers (3)

GHP
GHP

Reputation: 3231

"I want to create a view in sql server database that must have the Where clause. and when i will drag it to .Net dbml file, and use some where then must ask the parameter. can this possible."

I think you are asking if you can pass a "where" parameter to the View when it is called. This is not possible, but you can pass parameters to a Stored Procedure. Also, your stored procedure can query the View and use the param to filter it down. Here's an example:

VIEW: named "PersonView" -- gives you everything (no Where clause)

SELECT     cit.CitizenID, cit.FirstName, cit.LastName, cit.OrganizationID, org.Name AS 'OrganizationName', 
FROM   Citizens AS cit JOIN Organizations AS org ON cit.OrganizationID = org.ID

STORED PROCEDURE: named "spPersonQuery" -- does your Where clause

CREATE PROCEDURE [dbo].[spTest]
    -- Add the parameters for the stored procedure here
    @orgID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * from dbo.PersonView as ps
    where ps.OrganizationID = @orgID
END
GO

You can then drag your Stored Procedure into your DBML file and when you call it, you will pass in the int param "orgID".

Upvotes: 2

Chris Hogan
Chris Hogan

Reputation: 868

Yes, you can use a view in a .NET application. You use it in the same way you would use a table.

For example:

var result = from v in TestView select v;

Upvotes: 2

Banford
Banford

Reputation: 2569

Yes, just drag the View from server explorer in visual studio onto your linq2sql database model.

Upvotes: 2

Related Questions