-6

How can I write this stored procedure with EF in C#?

CREATE PROCEDURE FilterDocumentByNumber
    @Number NVARCHAR(30)
AS
BEGIN
    SELECT *
    FROM Document
    WHERE DocumentNumber = @Number
      AND CostGroupId IN (SELECT CostGroupId
                          FROM CostGroups
                          WHERE CostGroupType = 1)
END

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    You might find it helpful to read https://stackoverflow.com/help/how-to-ask and include a bit more information in your question, so that we know what you've tried and can help you out more. – Tim Aug 31 '17 at 11:51
  • Possible duplicate of [Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach](https://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity-framework-4-1-code-firs) – Peter B Aug 31 '17 at 11:51
  • 2
    If you are unfamiliar with Entity Framework, I suggest you start with something a little more simple - for example fetching a list of records from the database. – Lars Kristensen Aug 31 '17 at 11:51

1 Answers1

1

SO is not a website when you throw everything here and expected people finish the job for you.

Anyway, to give you some hint, I give you a straight suggestion:

Select CostGroupId From CostGroups Where CostGroupType = 1 --> Stored these in A collection, like an array:

var costGroupsIdArr = ctx.CostGroup.Where(x=>x.CostGroupType == 1).Select(x.CostGroupId).toArray();

Then from here, get your main query:

Select  *
From    Document
Where   DocumentNumber  =   @Number

Here is the last result:

var result = ctx.Document.Where(x=>x.DocumentNumber == number && vostGroupsIdArr.Contains(x.CostGroupId)).ToList();

You can try to figured out yourself what else needed from my snippet

Jacky
  • 2,924
  • 3
  • 22
  • 34