Write Apex Trigger more efficiently - apex-code

I am writing a trigger on an object (Object1__c) that will split a single row inserted into Object1__c
into multiple rows and do an insert into another object i.e. Object2__c
Basically a single line corresponding to 12 months (a year) of data has to be split into 12 monthly rows of data for the other object.
Currently I am using the crude way of splitting up as below.
Is there any efficient way of achieving this?
Trigger trgSplit on Object1__c (After Insert){
List<Object2__c> object2List = new List<Object2__c>;
For(Object1__c nc : [select Account_Number__c, Account_Name__c, Y1M01__c,Y1M03__c,Y1M04__c,Y1M05__c,Y1M06__c,Y1M07__c,Y1M08__c,Y1M09__c,Y1M10__c,Y1M11,Y1M12__c]) {
Object2__c pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M01__c;
pc.Record_Date__c = date.parse('01/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M02__c;
pc.Record_Date__c = date.parse('02/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M03__c;
pc.Record_Date__c = date.parse('03/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M04__c;
pc.Record_Date__c = date.parse('04/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M05__c;
pc.Record_Date__c = date.parse('05/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M06__c;
pc.Record_Date__c = date.parse('06/01/2014');
object2List.add(pc);
.............................................
//and so on
//upto
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M11__c;
pc.Record_Date__c = date.parse('11/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M12__c;
pc.Record_Date__c = date.parse('12/01/2014');
object2List.add(pc);
}
insert object2List;
}

You can get at sObject fields directly via the property name nc.Y1M01__c, or by the underlying sobject's get method nc.get('Y1M01__c'). Since you can pass a string dynamically you can now use a loop to get rid of the code re-use:
List<Object2__c> object2List = new List<Object2__c>;
For(Object1__c nc : [select Account_Number__c, Account_Name__c, Y1M01__c,Y1M03__c,Y1M04__c,Y1M05__c,Y1M06__c,Y1M07__c,Y1M08__c,Y1M09__c,Y1M10__c,Y1M11,Y1M12__c]) {
//loop thru each month, 1...12
for(Integer i = 1; i < 13; i++) {
//get the month and prefix a zero if necessary
String month = String.valueOf(i);
month = (month.length() == 1 ? '0' : '') + month;
//build the date
Date recDate = Date.parse(String.format('{0}/01/2014', new List<String> { month }));
//get the field api name
String y1mXX = String.format('Y1M{0}__c', new List<String> { month }));
//get the field value, you will need to cast it to the appropriate data type
Decimal y1mXXval = Decimal.valueOf( String.valueOf( nc.get(y01mXX) ) );
//since you are not using the new Object2__c anymore just pass it directly into the list
object2List.add(new Object2__c(
Account_Number__c = nc.Account_Number__c,
Account_Name__c = nc.Account_Name__c,
Total__c = y1mXXval,
Record_Date__c = recDate
));
}
}
insert object2List;

Related

Visual Studio is stuck at loading desinger, and it's not loading the form design

The problem happens when I load one of my team project solution it keeps loading none stop, but when I open a new project it loads normally and I can do whatever loading designer
I've been looking for days but they only thing I can find is when you get error loading massage
and this is the code I can see not the one my team mate wrote
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.Panel2 = New System.Windows.Forms.Panel()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.MenuStrip2 = New System.Windows.Forms.MenuStrip()
Me.HfdhToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الهيكلالتنظيميToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.المديرToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.المساعدToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.القوانينToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الانظمةToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.التعليماتToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الاسسToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.القراراتToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الاتفاقياتToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.اعرفحقكToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الطلبةToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.الموظفينToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.القظاياToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.قضايااداريةToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.قضاياالطلبةToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.قضاياالموظفينToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.قراراتالمحاكمToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.تسجيلخروجToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Panel2.SuspendLayout()
Me.MenuStrip2.SuspendLayout()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Panel2
'
Me.Panel2.BackColor = System.Drawing.Color.White
Me.Panel2.Controls.Add(Me.Label2)
Me.Panel2.Controls.Add(Me.Label1)
Me.Panel2.Dock = System.Windows.Forms.DockStyle.Top
Me.Panel2.Location = New System.Drawing.Point(0, 36)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(1156, 123)
Me.Panel2.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.BackColor = System.Drawing.Color.White
Me.Label2.Font = New System.Drawing.Font("Segoe UI", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.Label2.ForeColor = System.Drawing.Color.Black
Me.Label2.Location = New System.Drawing.Point(3, 17)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(63, 23)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Label2"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Label1.Font = New System.Drawing.Font("Arial Black", 36.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.Label1.ForeColor = System.Drawing.Color.Black
Me.Label1.Location = New System.Drawing.Point(206, 17)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(758, 85)
Me.Label1.TabIndex = 0
Me.Label1.Text = "جامعة مؤتة - الشؤون القانونية"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'MenuStrip2
'
Me.MenuStrip2.BackColor = System.Drawing.Color.White
Me.MenuStrip2.ImageScalingSize = New System.Drawing.Size(20, 20)
Me.MenuStrip2.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.اعرفحقكToolStripMenuItem, Me.HfdhToolStripMenuItem, Me.القوانينToolStripMenuItem, Me.القظاياToolStripMenuItem, Me.قراراتالمحاكمToolStripMenuItem, Me.تسجيلخروجToolStripMenuItem})
Me.MenuStrip2.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip2.Name = "MenuStrip2"
Me.MenuStrip2.Size = New System.Drawing.Size(1156, 36)
Me.MenuStrip2.TabIndex = 3
Me.MenuStrip2.Text = "MenuStrip2"
'
'HfdhToolStripMenuItem
'
Me.HfdhToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.HfdhToolStripMenuItem.BackColor = System.Drawing.Color.White
Me.HfdhToolStripMenuItem.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center
Me.HfdhToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.الهيكلالتنظيميToolStripMenuItem, Me.المديرToolStripMenuItem, Me.المساعدToolStripMenuItem, Me.المهامالتيتقومبهاالدائرةToolStripMenuItem})
Me.HfdhToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.HfdhToolStripMenuItem.ForeColor = System.Drawing.Color.Black
Me.HfdhToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.White
Me.HfdhToolStripMenuItem.Name = "HfdhToolStripMenuItem"
Me.HfdhToolStripMenuItem.Size = New System.Drawing.Size(158, 32)
Me.HfdhToolStripMenuItem.Text = "معلومات الدائرة"
Me.HfdhToolStripMenuItem.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage
'
'الهيكلالتنظيميToolStripMenuItem
'
Me.الهيكلالتنظيميToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.الهيكلالتنظيميToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.الهيكلالتنظيميToolStripMenuItem.Name = "الهيكلالتنظيميToolStripMenuItem"
Me.الهيكلالتنظيميToolStripMenuItem.Size = New System.Drawing.Size(241, 32)
Me.الهيكلالتنظيميToolStripMenuItem.Text = "الهيكل التنظيمي "
'
'المديرToolStripMenuItem
'
Me.المديرToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.المديرToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.المديرToolStripMenuItem.Name = "المديرToolStripMenuItem"
Me.المديرToolStripMenuItem.Size = New System.Drawing.Size(241, 32)
Me.المديرToolStripMenuItem.Text = "المدير"
'
'المساعدToolStripMenuItem
'
Me.المساعدToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.المساعدToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.المساعدToolStripMenuItem.Name = "المساعدToolStripMenuItem"
Me.المساعدToolStripMenuItem.Size = New System.Drawing.Size(241, 32)
Me.المساعدToolStripMenuItem.Text = "المساعد"
'
'المهامالتيتقومبهاالدائرةToolStripMenuItem
'
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem.Name = "المهامالتيتقومبهاالدائرةToolStripMenuItem"
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem.Size = New System.Drawing.Size(241, 32)
Me.المهامالتيتقومبهاالدائرةToolStripMenuItem.Text = "مهام الدائرة"
'
'القوانينToolStripMenuItem
'
Me.القوانينToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.القوانينToolStripMenuItem.BackColor = System.Drawing.Color.White
Me.القوانينToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.الانظمةToolStripMenuItem, Me.التعليماتToolStripMenuItem, Me.الاسسToolStripMenuItem, Me.القراراتToolStripMenuItem, Me.الاتفاقياتToolStripMenuItem})
Me.القوانينToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.القوانينToolStripMenuItem.ForeColor = System.Drawing.Color.Black
Me.القوانينToolStripMenuItem.Name = "القوانينToolStripMenuItem"
Me.القوانينToolStripMenuItem.Size = New System.Drawing.Size(90, 32)
Me.القوانينToolStripMenuItem.Text = "القوانين"
'
'الانظمةToolStripMenuItem
'
Me.الانظمةToolStripMenuItem.Name = "الانظمةToolStripMenuItem"
Me.الانظمةToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.الانظمةToolStripMenuItem.Text = "الانظمة"
'
'التعليماتToolStripMenuItem
'
Me.التعليماتToolStripMenuItem.Name = "التعليماتToolStripMenuItem"
Me.التعليماتToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.التعليماتToolStripMenuItem.Text = "التعليمات"
'
'الاسسToolStripMenuItem
'
Me.الاسسToolStripMenuItem.Name = "الاسسToolStripMenuItem"
Me.الاسسToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.الاسسToolStripMenuItem.Text = "الأسس"
'
'القراراتToolStripMenuItem
'
Me.القراراتToolStripMenuItem.Name = "القراراتToolStripMenuItem"
Me.القراراتToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.القراراتToolStripMenuItem.Text = "القرارات"
'
'الاتفاقياتToolStripMenuItem
'
Me.الاتفاقياتToolStripMenuItem.Name = "الاتفاقياتToolStripMenuItem"
Me.الاتفاقياتToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.الاتفاقياتToolStripMenuItem.Text = "الاتفاقيات"
'
'اعرفحقكToolStripMenuItem
'
Me.اعرفحقكToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.الطلبةToolStripMenuItem, Me.الموظفينToolStripMenuItem})
Me.اعرفحقكToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.اعرفحقكToolStripMenuItem.ForeColor = System.Drawing.Color.Black
Me.اعرفحقكToolStripMenuItem.Name = "اعرفحقكToolStripMenuItem"
Me.اعرفحقكToolStripMenuItem.Size = New System.Drawing.Size(114, 32)
Me.اعرفحقكToolStripMenuItem.Text = "اعرف حقك"
'
'الطلبةToolStripMenuItem
'
Me.الطلبةToolStripMenuItem.Name = "الطلبةToolStripMenuItem"
Me.الطلبةToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.الطلبةToolStripMenuItem.Text = "الطلبة"
'
'الموظفينToolStripMenuItem
'
Me.الموظفينToolStripMenuItem.Name = "الموظفينToolStripMenuItem"
Me.الموظفينToolStripMenuItem.Size = New System.Drawing.Size(224, 32)
Me.الموظفينToolStripMenuItem.Text = "الموظفين"
'
'القظاياToolStripMenuItem
'
Me.القظاياToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.القظاياToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.قضايااداريةToolStripMenuItem, Me.قضاياالطلبةToolStripMenuItem, Me.قضاياالموظفينToolStripMenuItem})
Me.القظاياToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.القظاياToolStripMenuItem.ForeColor = System.Drawing.Color.Black
Me.القظاياToolStripMenuItem.Name = "القظاياToolStripMenuItem"
Me.القظاياToolStripMenuItem.Size = New System.Drawing.Size(85, 32)
Me.القظاياToolStripMenuItem.Text = "القضايا"
'
'قضايااداريةToolStripMenuItem
'
Me.قضايااداريةToolStripMenuItem.Name = "قضايااداريةToolStripMenuItem"
Me.قضايااداريةToolStripMenuItem.Size = New System.Drawing.Size(232, 32)
Me.قضايااداريةToolStripMenuItem.Text = "قضايا ادارية"
'
'قضاياالطلبةToolStripMenuItem
'
Me.قضاياالطلبةToolStripMenuItem.Name = "قضاياالطلبةToolStripMenuItem"
Me.قضاياالطلبةToolStripMenuItem.Size = New System.Drawing.Size(232, 32)
Me.قضاياالطلبةToolStripMenuItem.Text = "قضايا الطلبة"
'
'قضاياالموظفينToolStripMenuItem
'
Me.قضاياالموظفينToolStripMenuItem.Name = "قضاياالموظفينToolStripMenuItem"
Me.قضاياالموظفينToolStripMenuItem.Size = New System.Drawing.Size(232, 32)
Me.قضاياالموظفينToolStripMenuItem.Text = "قضايا الموظفين"
'
'قراراتالمحاكمToolStripMenuItem
'
Me.قراراتالمحاكمToolStripMenuItem.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right
Me.قراراتالمحاكمToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.قراراتالمحاكمToolStripMenuItem.ForeColor = System.Drawing.Color.Black
Me.قراراتالمحاكمToolStripMenuItem.Name = "قراراتالمحاكمToolStripMenuItem"
Me.قراراتالمحاكمToolStripMenuItem.Size = New System.Drawing.Size(151, 32)
Me.قراراتالمحاكمToolStripMenuItem.Text = "قرارات المحاكم "
'
'تسجيلخروجToolStripMenuItem
'
Me.تسجيلخروجToolStripMenuItem.Font = New System.Drawing.Font("Segoe UI", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.تسجيلخروجToolStripMenuItem.Name = "تسجيلخروجToolStripMenuItem"
Me.تسجيلخروجToolStripMenuItem.Size = New System.Drawing.Size(112, 32)
Me.تسجيلخروجToolStripMenuItem.Text = "تسجيل خروج"
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.White
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Bottom
Me.PictureBox1.Location = New System.Drawing.Point(0, 156)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(1156, 524)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox1.TabIndex = 4
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 20.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(1156, 680)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.MenuStrip2)
Me.Name = "Form1"
Me.Text = "home page"
Me.Panel2.ResumeLayout(False)
Me.Panel2.PerformLayout()
Me.MenuStrip2.ResumeLayout(False)
Me.MenuStrip2.PerformLayout()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Panel2 As Panel
Friend WithEvents Label1 As Label
Friend WithEvents MenuStrip2 As MenuStrip
Friend WithEvents HfdhToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الهيكلالتنظيميToolStripMenuItem As ToolStripMenuItem
Friend WithEvents المديرToolStripMenuItem As ToolStripMenuItem
Friend WithEvents المساعدToolStripMenuItem As ToolStripMenuItem
Friend WithEvents المهامالتيتقومبهاالدائرةToolStripMenuItem As ToolStripMenuItem
Friend WithEvents القوانينToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الانظمةToolStripMenuItem As ToolStripMenuItem
Friend WithEvents التعليماتToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الاسسToolStripMenuItem As ToolStripMenuItem
Friend WithEvents القراراتToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الاتفاقياتToolStripMenuItem As ToolStripMenuItem
Friend WithEvents PictureBox1 As PictureBox
Friend WithEvents اعرفحقكToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الطلبةToolStripMenuItem As ToolStripMenuItem
Friend WithEvents الموظفينToolStripMenuItem As ToolStripMenuItem
Friend WithEvents القظاياToolStripMenuItem As ToolStripMenuItem
Friend WithEvents قضايااداريةToolStripMenuItem As ToolStripMenuItem
Friend WithEvents قضاياالطلبةToolStripMenuItem As ToolStripMenuItem
Friend WithEvents قضاياالموظفينToolStripMenuItem As ToolStripMenuItem
Friend WithEvents قراراتالمحاكمToolStripMenuItem As ToolStripMenuItem
Friend WithEvents Label2 As Label
Friend WithEvents تسجيلخروجToolStripMenuItem As ToolStripMenuItem
End Class

linq sum of multiple values

i need to get the sum of billableHours and nonBillableHours.
this is my code.
var currentMonth = 10;
var userQuery =
from timeEntry in TimeEntries
join ta in Tasks on timeEntry.TaskID equals ta.TaskID
where timeEntry.DateEntity.Month == currentMonth && timeEntry.DateEntity.Year == DateTime.Today.Year
select new
{
HoursEntered = timeEntry.HoursEntered,
Billable = ta.Billable
};
var localrows = userQuery.ToList();
var grouping = localrows.GroupBy(x => x.Billable);
var userList = grouping.Select(q => new
{
billableHours = q.Where(x=> x.Billable == true),
nonBillableHours = q.Where(x=> x.Billable != true)
});
i cannot seem to find a way to get the sum.
I need the sum of those two columns, so i can call them,
and calculate values i get from them.
When you need more than one aggregate, you can still get the result with a single query by using group by constant technique. Which in this specific case can be combined with conditional Sum:
var hoursInfo =
(from timeEntry in TimeEntries
join ta in Tasks on timeEntry.TaskID equals ta.TaskID
where timeEntry.DateEntity.Month == currentMonth && timeEntry.DateEntity.Year == DateTime.Today.Year
group new { timeEntry.HoursEntered, ta.Billable } by 1 into g
select new
{
BillableHours = g.Sum(e => e.Billable ? e.HoursEntered : 0),
NonBillableHours = g.Sum(e => !e.Billable ? e.HoursEntered : 0),
}).FirstOrDefault();
You do not need to group them. Try this query:
var userQuery =
from timeEntry in TimeEntries
join ta in Tasks on timeEntry.TaskID equals ta.TaskID
where timeEntry.DateEntity.Month == currentMonth
&& timeEntry.DateEntity.Year == DateTime.Today.Year
select new
{
HoursEntered = timeEntry.HoursEntered,
Billable = ta.Billable
};
var billableHours = userQuery
.Where(m => m.Billable) // Billable
.Select(m => m.HoursEntered)
.DefaultIfEmpty(0)
.Sum();
var nonBillableHours = userQuery
.Where(m => !m.Billable) // Non-bilable
.Select(m => m.HoursEntered)
.DefaultIfEmpty(0)
.Sum();
var currentMonth = 10;
var TimeEntries = new List<TimeEntry>() {
new TimeEntry(){TaskID = 1,DateEntity = DateTime.Now.AddDays(1),HoursEntered =2},
new TimeEntry(){TaskID = 2,DateEntity = DateTime.Now.AddDays(2),HoursEntered =3},
new TimeEntry(){TaskID = 3,DateEntity = DateTime.Now.AddDays(3),HoursEntered =2},
new TimeEntry(){TaskID = 4,DateEntity = DateTime.Now.AddDays(4),HoursEntered =4},
new TimeEntry(){TaskID = 5,DateEntity = DateTime.Now.AddDays(5),HoursEntered =2},
new TimeEntry(){TaskID = 6,DateEntity = DateTime.Now.AddDays(6),HoursEntered =6}
};
var UserTasks = new List<UserTask>(){
new UserTask(){TaskID = 1,Billable = true} ,
new UserTask(){TaskID = 2,Billable = false} ,
new UserTask(){TaskID = 3,Billable = true} ,
new UserTask(){TaskID = 4,Billable = false} ,
new UserTask(){TaskID = 5,Billable = true} ,
new UserTask(){TaskID = 6,Billable = false}
};
var userQuery =
from x in
(from timeEntry in TimeEntries
join ta in UserTasks on timeEntry.TaskID equals ta.TaskID
where timeEntry.DateEntity.Month == currentMonth && timeEntry.DateEntity.Year == DateTime.Today.Year
select new
{
HoursEntered = timeEntry.HoursEntered,
Billable = ta.Billable
})
group x by x.Billable into g
select new
{
IsBillable = g.Key,
Billabe = g.Where(t => t.Billable == true).Sum(x => x.HoursEntered),
NonBillable = g.Where(t => t.Billable == false).Sum(x => x.HoursEntered)
};
foreach (var item in userQuery.ToList())
{
Console.WriteLine(string.Format("{0} - {1}", item.IsBillable? "Billable":"Non-Billable",item.IsBillable?item.Billabe:item.NonBillable));
}

Must declare the scalar variable "#historyId" exception

I have a working raw Sql command execution
using (var cmd = SqlUtils.CreateSqlCommand(cmdText, sqlConn))
{
cmd.Parameters.Add(new SqlParameter("historyId", SqlDbType.Int)).Value = AAverage.CashFlowRelevantHistoryId;
cmd.Parameters.Add(new SqlParameter("nodeId", SqlDbType.Int)).Value = AAverage.CashNodeId;
cmd.Parameters.Add(new SqlParameter("denomId", SqlDbType.Int)).Value = AAverage.DenominationId;
cmd.Parameters.Add(new SqlParameter("dayId", SqlDbType.Int)).Value = AAverage.CashFlowDayCategoryId;
cmd.Parameters.Add(new SqlParameter("hour", SqlDbType.Int)).Value = AAverage.Hour;
cmd.Parameters.Add(new SqlParameter("type", SqlDbType.Int)).Value = AAverage.ValueType;
cmd.Parameters.Add(new SqlParameter("average", SqlDbType.Real)).Value = AAverage.AverageDerivative;
using (var reader = cmd.ExecuteReader())
if (reader.HasRows && reader.Read())
{
IDataRecord record = (IDataRecord)reader;
AAverage.Id = record.GetInt32ByName("Id");
}
}
but if I've changed it to use SqlQuery method, I get the error in the subject:
StringBuilder cmdText = new StringBuilder();
cmdText.AppendLine("INSERT INTO CashFlowAverageDerivatives");
cmdText.AppendLine(" (CashFlowRelevantHistoryId, CashNodeId, DenominationId, CashFlowDayCategoryId, Hour, ValueType, AverageDerivative)");
cmdText.AppendLine("VALUES");
cmdText.AppendLine(" (#historyId, #nodeId, #denomId, #dayId, #hour, #type, #average)");
cmdText.AppendLine("SELECT Id FROM CashFlowAverageDerivatives WHERE ##ROWCOUNT > 0 and Id = scope_identity()");
AAverage.Id = ADbContext.Database.SqlQuery<int>(
cmdText.ToString(),
new SqlParameter("historyId", SqlDbType.Int).Value = AAverage.CashFlowRelevantHistoryId,
new SqlParameter("nodeId", SqlDbType.Int).Value = AAverage.CashNodeId,
new SqlParameter("denomId", SqlDbType.Int).Value = AAverage.DenominationId,
new SqlParameter("dayId", SqlDbType.Int).Value = AAverage.CashFlowDayCategoryId,
new SqlParameter("hour", SqlDbType.Int).Value = AAverage.Hour,
new SqlParameter("type", SqlDbType.Int).Value = AAverage.ValueType,
new SqlParameter("average", SqlDbType.Real).Value = AAverage.AverageDerivative
).First<int>();
and I have no idea why. If I tried to rename "historyId" in Sql text to something else, exception reports this new name. It seems like parameter definition was missing or misspelled, but list of parameters has been copy&pasted, VS IDE search command finds it, prefix "#" does not matter...
Sure I can live with my working version, but I'm learning EF and it tease me I'm not able to solve this
You were sending the sql parameter as value directly.
When you did this
new SqlParameter("historyId", SqlDbType.Int).Value = AAverage.CashFlowRelevantHistoryId
You actually sent a value of AAverage.CashFlowRelevantHistoryId, not a SqlParameter.
Try changing it with this and see if it works.
AAverage.Id = ADbContext.Database.SqlQuery<int>(
cmdText.ToString(),
new SqlParameter("historyId", AAverage.CashFlowRelevantHistoryId),
new SqlParameter("nodeId", AAverage.CashNodeId),
new SqlParameter("denomId", AAverage.DenominationId),
new SqlParameter("dayId", AAverage.CashFlowDayCategoryId),
new SqlParameter("hour", AAverage.Hour),
new SqlParameter("type", AAverage.ValueType),
new SqlParameter("average", AAverage.AverageDerivative)
).First<int>();
Or this
AAverage.Id = ADbContext.Database.SqlQuery<int>(
cmdText.ToString(),
new SqlParameter("historyId", SqlDbType.Int) { Value = AAverage.CashFlowRelevantHistoryId },
new SqlParameter("nodeId", SqlDbType.Int) { Value = AAverage.CashNodeId },
new SqlParameter("denomId", SqlDbType.Int) { Value = AAverage.DenominationId },
new SqlParameter("dayId", SqlDbType.Int) { Value = AAverage.CashFlowDayCategoryId },
new SqlParameter("hour", SqlDbType.Int) { Value = AAverage.Hour },
new SqlParameter("type", SqlDbType.Int) { Value = AAverage.ValueType },
new SqlParameter("average", SqlDbType.Real) { Value = AAverage.AverageDerivative }
).First<int>();

Magento SOAP V_2 creating order error: Payment not allowed

I am trying to create an order on Magento 1.7.2 using SOAP API V_2 using .NET consol.
I am always getting the same error "Payment not allowerd" using every payment method.
//create an order with Magento API
MagentoService proxy = new MagentoService();
string sessionId = proxy.login("xxx", "xxx");
int idCarrello = proxy.shoppingCartCreate(sessionId, "1");
proxy.UnsafeAuthenticatedConnectionSharing = false;
shoppingCartCustomerEntity clienteMagento = new shoppingCartCustomerEntity();
clienteMagento.firstname = "name";
clienteMagento.lastname = "surname";
clienteMagento.email = "xxx#mmmm.com";
clienteMagento.mode = "guest";
proxy.shoppingCartCustomerSet(sessionId, idCarrello, clienteMagento, "1");
shoppingCartCustomerAddressEntity indirizzoSpedizione = new shoppingCartCustomerAddressEntity();
shoppingCartCustomerAddressEntity indirizzoBill = new shoppingCartCustomerAddressEntity();
indirizzoSpedizione.mode = "shipping";
indirizzoSpedizione.firstname = clienteMagento.firstname;
indirizzoSpedizione.lastname = clienteMagento.lastname;
indirizzoSpedizione.street = "viale europa 32";
indirizzoSpedizione.city = "Foggia";
indirizzoSpedizione.region = "FG";
indirizzoSpedizione.telephone = "111";
indirizzoSpedizione.postcode = "71122";
indirizzoSpedizione.country_id = "IT";
indirizzoSpedizione.is_default_billing = 0;
indirizzoSpedizione.is_default_shipping = 0;
indirizzoBill.mode = "billing";
indirizzoBill.firstname = clienteMagento.firstname;
indirizzoBill.lastname = clienteMagento.lastname;
indirizzoBill.street = "viale europa 32";
indirizzoBill.city = "Foggia";
indirizzoBill.region = "FG";
indirizzoBill.telephone = "111";
indirizzoBill.postcode = "71122";
indirizzoBill.country_id = "IT";
indirizzoBill.is_default_billing = 0;
indirizzoBill.is_default_shipping = 0;
shoppingCartCustomerAddressEntity[] indirizzi = new shoppingCartCustomerAddressEntity[] { indirizzoSpedizione, indirizzoBill };
proxy.shoppingCartCustomerAddresses(sessionId, idCarrello, indirizzi, "1");
proxy.shoppingCartShippingMethod(sessionId, idCarrello, "flatrate_flatrate", "1");
shoppingCartPaymentMethodResponseEntity[] paymentMethods = proxy.shoppingCartPaymentList(sessionId, idCarrello, "1");
Console.WriteLine(paymentMethods); //paymentMethods is always empty!!
shoppingCartPaymentMethodEntity modoPagamento = new shoppingCartPaymentMethodEntity();
modoPagamento.po_number = null;
modoPagamento.method = "checkmo";
modoPagamento.cc_cid = null;
modoPagamento.cc_owner = null;
modoPagamento.cc_number = null;
modoPagamento.cc_type = null;
modoPagamento.cc_exp_year = null;
modoPagamento.cc_exp_month = null;
proxy.shoppingCartPaymentMethod(sessionId, idCarrello, modoPagamento, "1");
Here is the Exception:
//Payment method is not allowed (I tryed checkmo, banktransfer, etc)
//proxy.shoppingCartOrder(sessionId, idCarrello, "1", new string[] { });
Any idea?
Ifaced with the saeme issue. This is the solution - You forgot product, just add product to sale. Also you must do it according to this order (it will not works in other order):
shoppingCartCustomerSet(guest for simplicity);
shoppingCartCustomerAddresses();
shoppingCartProductAdd();
shoppingCartShippingMethod();
shoppingCartPaymentMethod();

mvc3 dynamic report

Using MVC3 and razor along with NuGet Doddle Report, I have built a nice report that opens a view model in Excel. I would like to allow the user to supply a filter and sort on the view before the report is exported to Excel.
Are there recommended ways to do this or resources I could check out?
Thanks!
EDIT
Controller code that creates report:
public ReportResult CaseReport()
{
var viewModel = new CasesReportViewModel();
var query = (from c in db.Cases
join customer in db.Customers on c.CustomerID equals customer.CustomerID
join category in db.CaseCategories on c.CaseCategoryID equals category.CaseCategoryID
join tech in db.Technicians on c.TechnicianID equals tech.TechnicianID
join engine in db.EngineModels on c.EngineModelID equals engine.EngineModelID
select new CasesReportViewModel()
{
CustomerName = c.Customer.CustomerName,
UserName = c.UserName,
PromoID = tech.PromoID,
EngineModelName = engine.EngineModelName,
CaseNumber = c.CaseNumber,
BMSWorkorder = c.BMSWorkorder,
CaseStatus = c.CaseStatus,
OpenedBy = c.OpenedBy,
OpenedDate = c.OpenedDate,
ClosedBy = c.ClosedBy,
ClosedDate = c.ClosedDate,
CategoryName = category.CategoryName,
CallerFirstName = c.CallerFirstName,
CallerLastName = c.CallerLastName,
AdditionalContact = c.AdditionalContact,
Qualified = c.Qualified,
Description = c.Description,
ESN = c.ESN,
Mileage = c.Mileage,
DateInService = c.DateInService,
ESTR = c.ESTR,
EDS = c.EDS
});
var report = new Report(query.ToReportSource());
//customize fields
report.TextFields.Title = "Case Report";
AppHelpers app = new AppHelpers();
report.TextFields.Header = "Report Date = " + Convert.ToString(app.GetEasternTime());
report.TextFields.Footer = "Copyright 2012";
//data fields
report.RenderHints.BooleanCheckboxes = true;
report.DataFields["CustomerName"].DataFormatString = "{0:c}";
report.DataFields["UserName"].DataFormatString = "{0:c}";
report.DataFields["EngineModelName"].DataFormatString = "{0:c}";
report.DataFields["CaseNumber"].DataFormatString = "{0:c}";
report.DataFields["BMSWorkorder"].DataFormatString = "{0:c}";
report.DataFields["CategoryName"].DataFormatString = "{0:c}";
report.DataFields["CaseStatus"].DataFormatString = "{0:c}";
report.DataFields["OpenedBy"].DataFormatString = "{0:c}";
report.DataFields["OpenedDate"].DataFormatString = "{0:d}";
report.DataFields["ClosedBy"].DataFormatString = "{0:c}";
report.DataFields["ClosedDate"].DataFormatString = "{0:d}";
report.DataFields["CallerFirstName"].DataFormatString = "{0:c}";
report.DataFields["CallerLastName"].DataFormatString = "{0:c}";
report.DataFields["AdditionalContact"].DataFormatString = "{0:c}";
report.DataFields["Qualified"].DataFormatString = "{0:c}";
report.DataFields["Description"].DataFormatString = "{0:c}";
report.DataFields["ESN"].DataFormatString = "{0:c}";
report.DataFields["Mileage"].DataFormatString = "{0:c}";
report.DataFields["DateInService"].DataFormatString = "{0:d}";
report.DataFields["ESTR"].DataFormatString = "{0:c}";
report.DataFields["EDS"].DataFormatString = "{0:c}";
return new ReportResult(report, new ExcelReportWriter(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileName = "CaseReport" };
}

Resources