Click here to Skip to main content
15,891,033 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionException Pin
adigatm5-May-09 7:13
adigatm5-May-09 7:13 
AnswerRe: Exception Pin
Yusuf5-May-09 9:24
Yusuf5-May-09 9:24 
AnswerRe: Exception Pin
Abhijit Jana5-May-09 19:41
professionalAbhijit Jana5-May-09 19:41 
GeneralRe: Exception Pin
adigatm7-May-09 4:46
adigatm7-May-09 4:46 
QuestionLogin / sessions WITHOUT using MSSQL Roles / Users Pin
EliottA5-May-09 4:55
EliottA5-May-09 4:55 
QuestionHow to create Scheduled Tasks in dotnet code??? Pin
nkmkrishna5-May-09 4:48
nkmkrishna5-May-09 4:48 
AnswerRe: How to create Scheduled Tasks in dotnet code (has nothing to do with ASP.NET) Pin
led mike5-May-09 4:56
led mike5-May-09 4:56 
QuestionCompiled LINQ Queries Pin
John Gathogo5-May-09 4:37
John Gathogo5-May-09 4:37 
Lets start here. I have the following two classes:

public class Order
{
    public Int32 Id { get; set; }
    public String OrderNumber { get; set; }
    public DateTime OrderDate { get; set; }
    public String OrderStatus { get; set; }
    public Int32 CustomerId { get; set; }
    public List<OrderItem> Items { get; set; }
    public Decimal Total { get; set; }
}

public class OrderItem
{
    public Int32 OrderId { get; set; }
    public Int32 OrderLineNumber { get; set; }
    public Int32 ItemId { get; set; }
    public Double OrderQty { get; set; }
    public Decimal Price { get; set; }
    public Decimal LineTotal { get; set; }
}



Here is some LINQ query that am using to fetch Orders from two database tables (OrderMaster and OrderDetail) and return a List<Order>. The LINQ DataContext that has the two tables is named TempContext.


static List<Order> GetOrders()
{
    using (var context = new TempContext())
    {
        return (from om in context.OrderMasters
                select new Order
                {
                    Id = om.OrderID,
                    OrderNumber = om.OrderNumber,
                    OrderDate = om.OrderDate,
                    OrderStatus = om.OrderStatus,
                    CustomerId = om.CustomerID,
                    Total = om.Total,
                    Items = (from od in context.OrderDetails.Where(d => d.OrderID == om.OrderID)
                             select new OrderItem
                             {
                                 OrderLineNumber = od.OrderLineNumber,
                                 OrderId = od.OrderID,
                                 ItemId = od.ItemID,
                                 OrderQty = od.OrderQty,
                                 Price = od.Price,
                                 LineTotal = od.Total
                             }).ToList()
                }).ToList();
    }
}



My idea is to convert this LINQ Query to a Compiled LINQ Query and see whether I will be able to get any notable improvements on execution performance. Here is what I was able to do:


static List<Order> GetCompiledOrders()
{
    using (var context = new TempContext())
    {
        return (from om in CompiledOrders(context)
                select new Order
                {
                    Id = om.OrderID,
                    OrderNumber = om.OrderNumber,
                    OrderDate = om.OrderDate,
                    OrderStatus = om.OrderStatus,
                    CustomerId = om.CustomerID,
                    Total = om.Total,
                    Items = (from od in CompiledItems(context, om.OrderID)
                             select new OrderItem
                             {
                                 OrderLineNumber = od.OrderLineNumber,
                                 OrderId = od.OrderID,
                                 OrderQty = od.OrderQty,
                                 ItemId = od.ItemID,
                                 Price = od.Price,
                                 LineTotal = od.Total
                             }).ToList()
                }).ToList();
    }
}

public static readonly Func<TempContext, IQueryable<OrderMaster>>
CompiledOrders =
CompiledQuery.Compile<TempContext, IQueryable<OrderMaster>>
((context)
=>
from om in context.OrderMasters
select om
);

public static readonly Func<TempContext, Int32, IQueryable<OrderDetail>>
CompiledItems =
CompiledQuery.Compile<TempContext, Int32, IQueryable<OrderDetail>>
((context, orderId)
=>
from od in context.OrderDetails
select od
);



The above code worked. Notice that I did two compiled queries. One to get me the Orders and the other to get me the OrderItems which I then combined in GetCompiledOrders method. I would like to know how else I can do this query so that I can use one compiled query and still have OrderItems [Items] as List<OrderItem> (not IQueryable<OrderItem> ) and still have GetCompiledOrders return List<Order>.


And yes, I googled. Most examples on how to create compiled queries cover returning a simple list (with no child collection). And yes, its possible that the NESTED LINQ way is the PROBABLY the best bet when it comes to these scenarios. Your thoughts are welcome. Thanks for your time. (And sorry for the long post).



Below is a script you can use to create OrderMaster and OrderDetail table.


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[OrderMaster](
	[OrderID] [int] IDENTITY(1,1) NOT NULL,
	[OrderNumber] [nvarchar](10) NOT NULL,
	[OrderDate] [datetime] NOT NULL,
	[CustomerID] [int] NOT NULL,
	[OrderStatus] [nvarchar](36) NOT NULL,
	[Total] [money] NOT NULL,
 CONSTRAINT [PK__OrderMaster] PRIMARY KEY CLUSTERED 
(
	[OrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[OrderMaster] ADD  CONSTRAINT [DF_OrderMaster_Total]  DEFAULT ((0.0)) FOR [Total]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[OrderDetail](
	[OrderID] [int] NOT NULL,
	[OrderLineNumber] [int] NOT NULL,
	[ItemID] [int] NOT NULL,
	[OrderQty] [float] NOT NULL,
	[Price] [money] NOT NULL,
	[Total] [money] NOT NULL,
 CONSTRAINT [PK__OrderDetail] PRIMARY KEY CLUSTERED 
(
	[OrderID] ASC,
	[OrderLineNumber] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[OrderDetail] ADD  CONSTRAINT [DF_OrderDetail_OrderQty]  DEFAULT ((1.0)) FOR [OrderQty]
GO

ALTER TABLE [dbo].[OrderDetail] ADD  CONSTRAINT [DF_OrderDetail_Price]  DEFAULT ((0.0)) FOR [Price]
GO

ALTER TABLE [dbo].[OrderDetail] ADD  CONSTRAINT [DF_OrderDetail_Total]  DEFAULT ((0.0)) FOR [Total]
GO

Questionhow to access control id with usercontrol in .aspx page Pin
MallikarjunaGupta5-May-09 4:34
MallikarjunaGupta5-May-09 4:34 
Questionsys.WebForms.PageRequestManagerServerErrorExcpetion code 12031 Pin
JacquesDP5-May-09 4:12
JacquesDP5-May-09 4:12 
AnswerRe: sys.WebForms.PageRequestManagerServerErrorExcpetion code 12031 Pin
JacquesDP5-May-09 5:05
JacquesDP5-May-09 5:05 
QuestionAsp.NET ImageButtons problem Pin
Hristiyan5-May-09 3:38
Hristiyan5-May-09 3:38 
QuestionReg Tracking Of Email Pin
sathakmusthafa5-May-09 2:42
sathakmusthafa5-May-09 2:42 
AnswerRe: Reg Tracking Of Email Pin
Yusuf5-May-09 3:02
Yusuf5-May-09 3:02 
QuestionProblem in response Pin
CrazyCoder265-May-09 2:28
CrazyCoder265-May-09 2:28 
AnswerRe: Problem in response Pin
Yusuf5-May-09 3:06
Yusuf5-May-09 3:06 
QuestionEdit gridview in web user cointrol Pin
benams5-May-09 2:25
benams5-May-09 2:25 
AnswerRe: Edit gridview in web user cointrol Pin
Herman<T>.Instance5-May-09 4:01
Herman<T>.Instance5-May-09 4:01 
GeneralRe: Edit gridview in web user cointrol Pin
benams5-May-09 20:53
benams5-May-09 20:53 
QuestionPass Session Variable Value to HTML page... Pin
Sr...Frank5-May-09 1:49
Sr...Frank5-May-09 1:49 
AnswerRe: Pass Session Variable Value to HTML page... Pin
Aman Bhullar5-May-09 2:33
Aman Bhullar5-May-09 2:33 
GeneralRe: Pass Session Variable Value to HTML page... Pin
Sr...Frank5-May-09 3:16
Sr...Frank5-May-09 3:16 
Questionopps concept [modified] Pin
madhan joe5-May-09 0:59
madhan joe5-May-09 0:59 
AnswerRe: opps concept Pin
SeMartens5-May-09 1:04
SeMartens5-May-09 1:04 
QuestionHtml validation Pin
Karthick_gc5-May-09 0:40
Karthick_gc5-May-09 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.