1. How did you implemented OAuth?
2. How will you stop a application from opening in second tab?
Visit the blog for .Net FAQ,.Net interview questions,ASP .Net FAQ, C# .Net FAQ,ASP .Net interview questions, interview question on .Net, interview questions on C#.
1. How did you implemented OAuth?
2. How will you stop a application from opening in second tab?
Database anomaly is normally the flaw in databases which occurs because of poor planning and storing everything in a flat database. Generally this is removed by the process of normalization which is performed by splitting/joining of tables.
Prof_ID | Prof_Name | Dept. | Course Group |
39404 | Ashish | Marketing | Sec A |
39445 | Sonam | Product | Sec B |
43576 | Anu Priya | Finance | Sec C |
54325 | Anu Priya | Finance | Sec C |
99823 | Anushka | HR | Sec D |
14325 | Anushka | HR | Sec E |
There are three types of database anomalies:
a) Insertion anomaly: An insertion anomaly occurs when we are not able to insert certain attribute in the database without the presence of other attribute. For example suppose any professor is hired but not immediately assigned any course group or any department may not get his/her place in such type of flat database mentioned above, if null entries are not allowed in the database. So in the case mentioned above removing such type of problems requires splitting of the database which is done by normalization.
b) Update anomaly: This occurs in case of data redundancy and partial update. In other words a correct update of database needs other actions such as addition, deletion or both. For example in the above table the department assigned to Anushka is an error because it needs to be updated at two different place to maintain consistency.
c) Deletion Anomaly: Deletion anomaly occurs where deletion some data is deleted because of deletion of some other data. For example if Section B is to be deleted then un-necessarily Sonam’s detail has to be deleted. So normalization is generally done before deleting any record from a flat database.
Source : https://www.mbaskool.com/business-concepts/it-and-systems/12909-database-anomaly.html
Angular ngAfterViewInit()
is the method of AfterViewInit
interface. ngAfterViewInit()
is a lifecycle hook that is called after Angular has fully initialized a component's views. ngAfterViewInit()
is used to handle any additional initialization tasks. Find the AfterViewInit
interface code from Angular doc.
interface AfterViewInit { ngAfterViewInit(): void }
ngAfterViewInit()
is used to access properties annotated with @ViewChild()
and @ViewChildren()
decorators.ngAfterViewInit()
is executed after Angular initializes the component's views and child views. The child view is the view that a directive is in. ngAfterViewInit()
is executed only once after the first call of ngAfterContentChecked()
life cycle hook. After ngAfterViewInit()
lifecycle hook, the ngAfterViewChecked()
is called. ngAfterContentChecked()
responds after Angular checks the content projected into the directive/component and ngAfterViewChecked()
responds after Angular checks the component's views and child views.ngAfterViewInit
can be used with @ViewChild()
and @ViewChildren()
properties. ngAfterContentInit()
can be used with @ContentChild
and @ContentChildren
properties.import { Directive, ViewContainerRef } from '@angular/core'; @Directive({ selector: '[cpMsg]' }) export class MessageDirective { constructor(public viewContainerRef: ViewContainerRef) { } }
import { Component, ViewChild, ViewChildren, AfterViewInit, TemplateRef, QueryList } from '@angular/core'; import { MessageDirective } from './message.directive'; @Component({ selector: 'app-message', template: ` <h3>@ViewChildren() and @ViewChild()</h3> <div cpMsg></div> <div cpMsg></div> <div cpMsg></div> <ng-template #msgTemp> Namaste! </ng-template> ` }) export class MessageComponent implements AfterViewInit { @ViewChildren(MessageDirective) private msgList: QueryList<MessageDirective>; @ViewChild('msgTemp') private msgTempRef: TemplateRef<any>; ngAfterViewInit() { console.log("this.msgList.length: " + this.msgList.length); this.msgList.forEach(messageDirective => messageDirective.viewContainerRef.createEmbeddedView(this.msgTempRef)); } }
ngAfterViewInit()
responds when the component's view and its child view is initialized, so here inside this method we are reading the msgTemp
template data and embedding it into the cpMsg
directive.When to use Include with Entity Framework and is it related to lazy loading?
Before jumping into the answer of when to use Include, let's have a look at the following simple example which contains three entities.
public class Customer { public int CustomerID { get; set; } public string Name { get; set; } public virtual List<Invoice> Invoices { get; set; } } public class Invoice { public int InvoiceID { get; set; } public DateTime Date { get; set; } public int CustomerID { get; set; } public virtual Customer Customer { get; set; } public virtual ICollection<Item> Items { get; set; } } public class Item { public int ItemID { get; set; } public string Name { get; set; } public int InvoiceID { get; set; } public virtual Invoice Invoice { get; set; } }
Now let's retrieve all the customers from a database and also iterate over their invoices as well and then print the invoice date.
using (var context = new MyContext()) { var list = context.Customers.ToList(); foreach (var customer in list) { Console.WriteLine("Customer Name: {0}", customer.Name); foreach (var customerInvoice in customer.Invoices) { Console.WriteLine("\tInvoice Date: {0}", customerInvoice.Date); } } }
If you look at the generated SQL, then you will see that one SQL query is executed for retrieving customers and then for each customer, another query is executed for retrieving the Invoices related to that customer. So, it means, if you have 1000 customers in your database then EF will execute 1000 queries for retrieving invoices for that 1000 customers.
EF generates the following query for retrieving customers.
SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name] FROM [dbo].[Customers] AS [Extent1]
And the following query is generated for retrieving invoices of a specific customer.
1SELECT
2 [Extent1].[Id] AS [Id],
3 [Extent1].[Date] AS [Date],
4 [Extent1].[CustomerId] AS [CustomerId]
5 FROM [dbo].[Invoices] AS [Extent1]
6 WHERE [Extent1].[CustomerId] = @EntityKeyValue1
The best way to avoid the select N+1 problem in Entity Framework is to use the Include method. It will create one query with needed data using SQL JOIN clause which is more efficient as compared to the previous one.
Let's update our query by using the Include method.
using (var context = new MyContext()) { var list = context.Customers .Include(c => c.Invoices) .ToList(); foreach (var customer in list) { Console.WriteLine("Customer Name: {0}", customer.Name); foreach (var customerInvoice in customer.Invoices) { Console.WriteLine("\tInvoice Date: {0}", customerInvoice.Date); } } }
In the above example, we are telling EF explicitly that besides Customers we also need their Invoices. The following is the SQL generated query:
1SELECT
2 [Project1].[Id] AS [Id],
3 [Project1].[Name] AS [Name],
4 [Project1].[C1] AS [C1],
5 [Project1].[Id1] AS [Id1],
6 [Project1].[Date] AS [Date],
7 [Project1].[CustomerId] AS [CustomerId]
8 FROM ( SELECT
9 [Extent1].[Id] AS [Id],
10 [Extent1].[Name] AS [Name],
11 [Extent2].[Id] AS [Id1],
12 [Extent2].[Date] AS [Date],
13 [Extent2].[CustomerId] AS [CustomerId],
14 CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
15 FROM [dbo].[Customers] AS [Extent1]
16 LEFT OUTER JOIN [dbo].[Invoices] AS [Extent2] ON [Extent1].[Id] = [Extent2].[CustomerId]
17 ) AS [Project1]
18 ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC
As you can see that Entity Framework has used LEFT OUTER JOIN
clause to get all needed data. Another important point is that using Include method in the context which supports lazy loading can prevent the n+1 problem.
1. What does include function of Linq do
Ans : https://interview-preparation-for-you.blogspot.com/2021/06/when-to-use-include-with-entity.html
2. How to write left join in linq query
Ans using defualtIfEmpty function
var q =
from c in categories
join p in products on c.Category equals p.Category into ps
from p in ps.DefaultIfEmpty()
select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };
3. What we will do in ngAfterViewInit
Ans : https://interview-preparation-for-you.blogspot.com/2021/06/ngafterviewinit.html
4. What we will do in ngDoCheck
5. Delete duplicate row except one
Delete from Employee m1 , Employee m2 where m1.Name=m2.Name and m1.CreatedDate<m2.CreateDate
Source : https://www.c-sharpcorner.com/article/sql-server-indexed-views/