Generic Grouping Using Linq - linq

EmployeeName EmployeeNumber AllowanceName Amount
ABDEL 002285 Housing 100.00
ABDEL 002285 Tickets 12.083
ABDEL 002285 Transportation 100.00
MOHAMED 001546 Tickets 150.00
MOHAMED 001546 Transportation 0.935
I need to convert this flat table to crosstabulation table to be like this
EmployeeName EmployeeNumber Housing Tickets Transportation
ABDEL 002285 100.00 12.083 100.00
MOHAMED 001546 0.000 150.00 0.935

Not really generic but maybe sufficient:
var query = empmloyee
.GroupBy(x => new { x.EmployeeName, x.EmployeeNumber })
.Select(gx => new {
EmployeeName = gx.Key.EmployeeName,
EmployeeNumber = gx.Key.EmployeeNumber,
Housing = gx.Where(x => x.AllowanceName == "Housing")
.Sum(x => x.Amount),
Tickets = gx.Where(x => x.AllowanceName == "Tickets")
.Sum(x => x.Amount),
Transportation = gx.Where(x => x.AllowanceName == "Transportation")
.Sum(x => x.Amount),
});

Related

Linq query averages

I can run this in Linqpad and it works fine but in VS when i run it the result throws errors because of the Avg, Max and Min statements. Can anyone advise how i need to change this to get the desired result?
tickets = from t in dbContext.TblOmTasks
join o in dbContext.TblOms on t.OMID equals o.OMID
join ls in dbContext.LkpStatusBasics on t.OMTaskStatus equals ls.ID
where t.OMID == SiteId
where ls.Status.Contains(status)
group t by new { Y = t.Created.Value.Date.Year, M = t.Created.Value.Date.Month } into grp
orderby grp.Key.M
select new TBS
{
Month = new DateTime(grp.Key.Y, grp.Key.M, 1).ToString("MMM", CultureInfo.InvariantCulture)
,Avg = grp.Average(g => Convert.ToInt32((g.Updated.HasValue ? g.Updated - g.Created : DateTime.Now - g.Created).Value.Days))
,Max = grp.Max(g => Convert.ToInt32((g.Updated.HasValue ? g.Updated - g.Created : DateTime.Now - g.Created).Value.Days))
,Min = grp.Min(g => Convert.ToInt32((g.Updated.HasValue ? g.Updated - g.Created : DateTime.Now - g.Created).Value.Days))
};
Looks like EF Core still can not translate Timestamp.Days. So use appropriate functions.
tickets =
from t in dbContext.TblOmTasks
join o in dbContext.TblOms on t.OMID equals o.OMID
join ls in dbContext.LkpStatusBasics on t.OMTaskStatus equals ls.ID
where t.OMID == SiteId
where ls.Status.Contains(status)
group t by new { Y = t.Created.Value.Date.Year, M = t.Created.Value.Date.Month } into grp
orderby grp.Key.M
select new TBS
{
Month = new DateTime(grp.Key.Y, grp.Key.M, 1).ToString("MMM", CultureInfo.InvariantCulture),
Avg = grp.Average(g => EF.Functions.DateDiffDay(g.Created, g.Updated ?? DateTime.Now),
Max = grp.Max(g => EF.Functions.DateDiffDay(g.Created, g.Updated ?? DateTime.Now),
Min = grp.Min(g => EF.Functions.DateDiffDay(g.Created, g.Updated ?? DateTime.Now)
};

get distinct record from 3 tables using join in codeigniter

i have three tables
cases,clients,attendance
in cases
id start_date end_date client_id status
1 2012-12-30 2013-01-30 1 new starts
2 2012-12-31 2013-01-31 2 probation
in clients
id Name dob gender status
1 TOM 1987-01-30 M A
2 JERRY 1985-01-31 F D
in attendance
id client_id date status
1 1 2013-01-30 A
2 1 2013-01-31 P
i need result like this
case_id case_start_date case_end_date client_id Name
att_date atte_status
1 2012-12-30 2013-01-30 1 TOM
2013-01-30,2013-01-31 A,P
is this possible? i'm a self learner and i don't have good idea in join query please any body help me....
try this..
$this->db->select('ca.*,ci.Name,a.date,a.status');
$this->db->from('cases'.' ca');
$this->db->join('clients'.' ci','ca.client_id=ci.id','left');
$this->db->join('attendance'.' a','a.client_id=ci.id','left');
return $this->db->get();
go through the user guide if u want to read more about join and active records...
Here's a very simple example to get you started with any number of join common fun for all
Construcor Code
Construct $joins as array:
$joins = array(
array(
'table' => 'table2',
'condition' => 'table2.id = table1.id',
'jointype' => 'LEFT'
),
);
Example function handling joins as an array:
public function get_joins($table, $columns, $joins)
{
$this->db->select($columns)->from($table);
if (is_array($joins) && count($joins) > 0)
{
foreach($joins as $k => $v)
{
$this->db->join($v['table'], $v['condition'], $v['jointype']);
}
}
return $this->db->get()->result_array();
}

LINQ and 2 datatables

I have 2 datatables in a dataset. One table has a list called CostTypes. Just an Id and Description field.
The other datatable is the master table and has many records and one of the columns is the cost type. There will be cost types that are not reference in this datatable. There is another column in this databale called cost.
What I am trying to do is get a summary by cost type with a total of the cost. But I want ALL cost types listed any values not in the master table will be zero.
CostType table
Id, Description
1,Marketing
2,Sales
3,Production
4,Service
Master table
Id, Cost, CostTypeId
1,10,1
2,120,1
3,40,3
So I would like to see a result in a datable (if possible) so I can bind to datagridview
Marketing 130
Sales 0
Production 40
Service 0
Thanks for the help everyone, this is what I came up from the answers - Can anyone suggest any improvements???
Also how can I convert the result in query1 into a datable???
var query1 =
from rowCT in costTypes.AsEnumerable()
from rowSTD in stdRates.AsEnumerable()
.Where( d => d.Field<int?>( "CostTypeId" ) == rowCT.Field<int?>( "CostTypeId" ) )
.DefaultIfEmpty()
group new { row0 = rowCT, row1 = rowSTD }
by rowCT.Field<string>( "Description" ) into g
select new
{
g.Key,
Cost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "Cost" ) ),
TotalCost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalCost" ) ),
TotalHours = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalHours" ) ),
TotalLabourCost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalLabourCost" ) )
}
;
Maybe something like this:
Test data:
DataTable dt=new DataTable();
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Description",typeof(string));
dt.Rows.Add(1,"Marketing");
dt.Rows.Add(2,"Sales");
dt.Rows.Add(3,"Production");
dt.Rows.Add(4,"Service");
DataTable dt2=new DataTable();
dt2.Columns.Add("Id",typeof(int));
dt2.Columns.Add("Cost",typeof(int));
dt2.Columns.Add("CostTypeId",typeof(int));
dt2.Rows.Add(1,10,1);
dt2.Rows.Add(2,120,1);
dt2.Rows.Add(3,40,1);
Linq query
var query=(
from row in dt.AsEnumerable()
from row1 in dt2.AsEnumerable()
.Where (d =>d.Field<int>("Id")==row.Field<int>("Id") )
.DefaultIfEmpty()
group new{row,row1}
by row.Field<string>("Description") into g
select new
{
g.Key,
Cost=g.Sum (x =>x.row1==null?0:x.row1.Field<int>("Cost"))
}
);
Result
Key Cost
Marketing 10
Sales 120
Production 40
Service 0
You can use the Sum extension method to compute the cost. It will return 0 if the collection is empty which is exactly what you want:
var costTypes = new DataTable("CostTypes");
costTypes.Columns.Add("Id", typeof(Int32));
costTypes.Columns.Add("Description", typeof(String));
costTypes.Rows.Add(1, "Marketing");
costTypes.Rows.Add(2, "Sales");
costTypes.Rows.Add(3, "Production");
costTypes.Rows.Add(4, "Service");
var costEntries = new DataTable("CostEntries");
costEntries.Columns.Add("Id", typeof(Int32));
costEntries.Columns.Add("Cost", typeof(Int32));
costEntries.Columns.Add("CostTypeId", typeof(Int32));
costEntries.Rows.Add(1, 10, 1);
costEntries.Rows.Add(2, 120, 1);
costEntries.Rows.Add(3, 40, 3);
var costs = costTypes
.Rows
.Cast<DataRow>()
.Select(
dr => new {
Id = dr.Field<Int32>("Id"),
Description = dr.Field<String>("Description")
}
)
.Select(
ct => new {
ct.Description,
TotalCost = costEntries
.Rows
.Cast<DataRow>()
.Where(ce => ce.Field<Int32>("CostTypeId") == ct.Id)
.Sum(ce => ce.Field<Int32>("Cost"))
}
);
The result is:
Description|TotalCost
-----------+---------
Marketing | 130
Sales | 0
Production | 40
Service | 0
You can create a new DataSet quite simply:
var costsDataTable = new DataTable("Costs");
costsDataTable.Columns.Add("Description", typeof(String));
costsDataTable.Columns.Add("TotalCost", typeof(Int32));
foreach (var cost in costs)
costsDataTable.Rows.Add(cost.Description, cost.TotalCost);
If the linear search performed by the Where in the code above is a concern you can improve the performance by creating a lookup table in advance:
var costEntriesLookup = costEntries
.Rows
.Cast<DataRow>()
.Select(
ce => new {
Cost = ce.Field<Int32>("Cost"),
CostTypeId = ce.Field<Int32>("CostTypeId")
}
)
.ToLookup(ce => ce.CostTypeId, ce => ce.Cost);
var costs = costTypes
.Rows
.Cast<DataRow>()
.Select(
dr => new {
Id = dr.Field<Int32>("Id"),
Description = dr.Field<String>("Description")
}
)
.Select(
ct => new {
ct.Description,
TotalCost = costEntriesLookup.Contains(ct.Id)
? costEntriesLookup[ct.Id].Sum()
: 0
}
);
I came up with a simpler bit of linq than others seemed to use. Thanks to Martin Liversage for the code to create the input data.
var costTypes = new DataTable("CostTypes");
costTypes.Columns.Add("Id", typeof(Int32));
costTypes.Columns.Add("Description", typeof(String));
costTypes.Rows.Add(1, "Marketing");
costTypes.Rows.Add(2, "Sales");
costTypes.Rows.Add(3, "Production");
costTypes.Rows.Add(4, "Service");
var costEntries = new DataTable("CostEntries");
costEntries.Columns.Add("Id", typeof(Int32));
costEntries.Columns.Add("Cost", typeof(Int32));
costEntries.Columns.Add("CostTypeId", typeof(Int32));
costEntries.Rows.Add(1, 10, 1);
costEntries.Rows.Add(2, 120, 1);
costEntries.Rows.Add(3, 40, 3);
var cte = costTypes.Rows.Cast<DataRow>();
var cee = costEntries.Rows.Cast<DataRow>();
var output = cte.Select(
ct => new {
Description = ct["Description"],
Sum = cee.Where(ce=>ce["CostTypeId"].Equals(ct["Id"])).Sum(ce=>(int)ce["Cost"])
}
);
This may lose efficiency on larger tables since each cost type will search the cost entry table whereas using grouping I suspect you only need one pass over the table. Personally I'd prefer the (to my mind) simpler looking code. It will depend on your use case though.

Something wrong with my LINQ to Entities query

I currently have a LINQ query:
public List<EventSchool> GetEventSchools(int eventID)
{
var eventSchools = db.EventSchools
.Include("Organisation")
.Where(e => e.EventID == eventID)
.ToList();
foreach (var ev in eventSchools)
{
if (db.EventSchoolKeyStages.Where(e => e.EventSchoolID == ev.EventSchoolID).Count() > 0)
{
int ks = db.EventSchoolKeyStages
.Where(e => ev.EventSchoolID == ev.EventSchoolID)
.Sum(e => e.Males + e.Females);
ev.StudentNumbers = ks;
}
}
return eventSchools;
}
When I inspect EventSchools, the student numbers for ALL items in the list shows as the first total.
For example, if I have 3 items in the list:
Item 1 - Males = 10, Females = 10
Item 2 - Males = 1, Females = 2
Item 3 - Males = 200, Females = 500
ALL items have a StudentNumbers of 20, rather than:
Item 1 - 20
Item 2 - 3
Item 3 - 700
Not sure what I'm doing wrong?
You have a typo here:
.Where(e => ev.EventSchoolID == ev.EventSchoolID)
This lambda will always be true. I suspect you meant
.Where(e => e.EventSchoolID == ev.EventSchoolID)
^^^
which is different in the indicated place.
You have an error in your query:
.Where(e => ev.EventSchoolID == ev.EventSchoolID)
Should be:
.Where(e => e.EventSchoolID == ev.EventSchoolID)

C# Foreach loop doesn't work as expected

I have a list of posts, and I have a list of opinions, 4 opinions users can vote for each post.
It's like:
POST1
Vote (opinion A | 10 votes), vote (opinion B | 2 votes), vote (opinion C | 1 votes), vote (opinion D | 0 votes)
POST2
Vote (opinion A | 3 votes), vote (opinion B | 4 votes), vote (opinion C | 5 votes), vote (opinion D | 7 votes)
For each post any user posts in his or her Diary, other users can vote 1 opinion.
In my controller I have this line to get the Diary Posts:
var diaryPosts = (from d in db.DiaryPosts
join e in db.EstadosDeAlma
on d.EstadosDeAlmaID equals e.ID
join u in db.User
on d.UserID equals u.ID
orderby d.ID descending
select new DiaryPostsSet
{
PostID = d.ID,
EstadoDeAlmaID = e.ID,
EstadoDeAlma = e.Title,
Author = u.Nickname,
Thumbnail = u.Thumbnail,
AuthorComment = d.Content,
Time = d.UpdateTime }).Take(6).ToList();
And I try to take for each DiaryPost, its opinions votes, and here is my problem. I have it like this:
List<ImpressionsSet> impressions = new List<ImpressionsSet>();
foreach (var item in diaryPosts)
{
impressions = (from i in db.Impressions
select new ImpressionsSet
{
ImpressionID = i.ID,
ImpressionTitle = i.Impression,
UrlSlug = i.UrlSlug,
DiaryPostID = item.PostID,
ImpressionNum = i.DiaryImpressions.Count(d => d.DiaryPostsID == item.PostID)
}).ToList();
}
But 'impressions' var gets only the last loop. I don't know how to solve that, cuz I had only a few experience with arrays and lists in C#4.0. I don't know if I could use that like impressions[n], I tried and it doesn't work.
I also need a sample code on how I would control the results of this loop in my Razor View.
I have this now:
#foreach (var imp in ViewBag.ImpressionsList)
{
if (item.PostID == imp.DiaryPostID)
{
<td>
#{ string cbName = imp.UrlSlug; }
#{ string impression = imp.ImpressionTitle; }
#{ string value = imp.DiaryPostID + "|" + imp.ImpressionID + "|" + Session["id"].ToString(); }
<a class="voto" href="javascript:;" onclick="PostImpressions('#value')">#impression</a>
</td>
<td>
<a class="num" href="javascript:;" onclick="PostImpressions('#value')">#imp.ImpressionNum</a>
</td>
}
}
But I'm not sure if it would really work, cuz I dont have the results for the 2 posts in my impressions var, it just get the last result.
Could anyone help me?
instead of:
impressions = ...
Do
impressions.AddRange( (from i in db.Impressions
select new ImpressionsSet
{
ImpressionID = i.ID,
ImpressionTitle = i.Impression,
UrlSlug = i.UrlSlug,
DiaryPostID = item.PostID,
ImpressionNum = i.DiaryImpressions.Count(d => d.DiaryPostsID == item.PostID)
}).ToList() );

Resources