开发者

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 V开发者_StackOverflow中文版iew in .Net, so that i can improve performance? I am writing a C# desktop application.


"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".


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜