0

I am trying to make an ebank, for myself, so it is not that serious. This is my structure: http://i60.tinypic.com/rig0sx.png

What I achieved so far: if a user logs in, and click on the Ebank actionlink, for him/her I display ALL of the data (I mean if logged in userId==1 I display all of the data with userId==2 too, this is what I want to change).

public ActionResult Index()
{

     var bankaccounts = db.BankAccounts.Include(b => b.Type).Include(b => b.Balance).Include(b => b.UserProfile);
     return View(bankaccounts.ToList());

}

This "var bankaccounts" row is what I have to change if I am right. If I put a breakpoint to the "return view(bankaccounts.ToList())" row I get the following data from the "bankaccounts":

    {
SELECT 
    [Extent1].[BankAccountId] AS [BankAccountId], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[AccountName] AS [AccountName], 
    [Extent1].[AccountType] AS [AccountType], 
    [Extent1].[AccountNumber] AS [AccountNumber], 
    [Extent1].[AccountStatus] AS [AccountStatus], 
    [Extent1].[AccountAvaiability] AS [AccountAvaiability], 
    [Extent2].[AccountType] AS [AccountType1], 
    [Extent2].[TypeName] AS [TypeName], 
    [Extent3].[BalanceId] AS [BalanceId], 
    [Extent3].[BalanceAmount] AS [BalanceAmount], 
    [Extent3].[TrustAmount] AS [TrustAmount], 
    [Extent4].[UserId] AS [UserId1], 
    [Extent4].[UserName] AS [UserName]
    FROM    [dbo].[BankAccount] AS [Extent1]
    INNER JOIN [dbo].[Type] AS [Extent2] ON [Extent1].[AccountType] = [Extent2].[AccountType]
    LEFT OUTER JOIN [dbo].[Balance] AS [Extent3] ON [Extent1].[BankAccountId] = [Extent3].[BalanceId]
    INNER JOIN [dbo].[UserProfile] AS [Extent4] ON [Extent1].[UserId] = [Extent4].[UserId]}

Questions:

I cannot find any real sql command in the code(its auto generated by mvc), so If I am right

  1. does this "var bankaccounts" row do the sql select?
  2. If yes, how?
  3. Where can I read after this type of programming?
  4. I want to change this code to something like "select ... where userid == websecurity. CurrentUserId", can you help me with this please?
  5. Any more ideas, advices that I should take or read after? The more you say the more I can learn!
2
  • Welcome to LINQ world, you can run sql scripts using DBQuery. checkout this -> msdn.microsoft.com/en-us/data/jj592907.aspx Commented Feb 14, 2014 at 11:46
  • You should Google the term "Object Relational Mapper". This answers all of your questions. Commented Feb 14, 2014 at 11:46

1 Answer 1

1

You are using a provider like Entity Framework which is providing the mapping from code to your database. Specifically:

  1. In your case, the Sql gets executed when you materialize the query with ToList(). Until the materialization step, var bankaccounts is an IQueryable.
  2. Entity Framework has a Query provider which knows how to parse the Expressions you give it to Sql statements, and similarly, to map result sets back to entities like BankAccount.
  3. Sites like MSDN will give you an overview
  4. Add a Where expression predicate:

var userBankaccounts = db.BankAccounts
                         .Where(ba => ba.UserId == websecurity.CurrentUserId);

The Sql you see is 'real' (sans the curly {} brackets) - once you look past the rather annoying aliasing of tables and derived tables as [Extentxxx], it will allow you to quickly check that your Linq expressions are performing the queries required.

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

Comments

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.