Linq query with datatable [closed] - linq

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have this datatable result in c#
Date Employee Job1 Job2 Job3
1/1/2012 a 1 1 1
1/1/2012 b 2 2 2
1/1/2012 c 2 1 4
1/1/2012 d 4 2 1
1/2/2012 a 3 2 5
1/2/2012 b 2 2 2
1/2/2012 c 3 3 3
1/2/2012 d 1 1 1
1/3/2012 a 5 5 5
1/3/2012 b 2 2 6
1/3/2012 c 1 1 1
1/3/2012 d 2 3 4
2/1/2012 a 2 2 2
2/1/2012 b 5 5 2
2/1/2012 c 2 2 2
2/2/2012 a 3 3 3
2/2/2012 b 2 3 3
3/1/2012 a 4 4 2
Now I want a result like this:
Job1:
Employee January February March
A 9 5 4
B 6 7
C 6 2
D 7
Please, can anybody suggest me how to do this with "Linq" in c#?

This might work (or give you at least an idea):
var monthEmpGroups = tblEmpJobs.AsEnumerable()
.Select(r => new
{
Row = r,
Employee = r.Field<String>("Employee"),
Year = r.Field<DateTime>("Date").Year,
Month = r.Field<DateTime>("Date").Month
})
.GroupBy(x => x.Employee);
DataTable tblMonthResultJob1 = new DataTable();
tblMonthResultJob1.Columns.Add("Employee", typeof(string));
var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
foreach (var empGroup in monthEmpGroups)
{
string employee = empGroup.Key;
var newRow = tblMonthResultJob1.Rows.Add();
newRow["Employee"] = employee;
var empMonthGroup = empGroup.GroupBy(mg => new { mg.Year, mg.Month });
foreach (var empYearMonthGroup in empMonthGroup)
{
int year = empYearMonthGroup.Key.Year;
int month = empYearMonthGroup.Key.Month;
string colName = string.Format("{0} {1}", dtf.GetMonthName(month), year);
if (!tblMonthResultJob1.Columns.Contains(colName))
tblMonthResultJob1.Columns.Add(colName, typeof(int));
int empJob1Count = empYearMonthGroup.Sum(x => x.Row.Field<int>("Job1"));
newRow[colName] = empJob1Count;
}
}
// do the same for the other job types
Update If you need to add a "total-row" at the "end" as commented:
DataRow totalRow = tblMonthResultJob1.Rows.Add();
totalRow["Employee"] = "ALL";
var monthGroups = tblEmpJobs.AsEnumerable()
.Select(r => new {
Row = r,
Year = r.Field<DateTime>("Date").Year,
Month = r.Field<DateTime>("Date").Month
})
.GroupBy(x => new { x.Year, x.Month });
foreach (var monthGroup in monthGroups)
{
int yearAll = monthGroup.Key.Year;
int monthAll = monthGroup.Key.Month;
string colName = string.Format("{0} {1}", dtf.GetMonthName(monthAll), yearAll);
if (!tblMonthResultJob1.Columns.Contains(colName))
tblMonthResultJob1.Columns.Add(colName, typeof(int));
int allJob1Count = monthGroup.Sum(x => x.Row.Field<int>("Job1"));
totalRow[colName] = allJob1Count;
}
Update2
I am getting error system.dbnull on this line int
empJob1Count = empYearMonthGroup.Sum(x => x.Row.Field<int>("Job1"));
I think there
are some null values and it is not able to cast null values while
doing sum. Please help, how should i resolve this
You can use this code to default an int? to zero:
var empYearMonthCount = empYearMonthGroup.Sum(x =>
{
int? job1 = x.Row.Field<int?>("Job1");
int value = 0;
if(job1.HasValue) value = job1.Value;
return value;
});

Related

linq query to list that adds position of result

I have the linq query that produces the result below:
var result = from x in model.SITEs
where x.SiteId == homeSite
select new { x.SiteId,
x.SiteAlloc1,
x.SiteAlloc2,
x.SiteAlloc3,
x.SiteAlloc4 });
SiteId SiteAlloc1 SiteAlloc2 SiteAlloc3 SiteAlloc4
======================================================
1 5 3 2 4
But what I need is something more like this, where the Rank is the position of the SiteId in the result.
SiteId Rank
==================
1 1
2 4
3 3
4 5
5 2
var result = from x in model.SITEs
where x.SiteId == homeSite
select new { x.SiteId,
x.SiteAlloc1,
x.SiteAlloc2,
x.SiteAlloc3,
x.SiteAlloc4 }).
Select((t,u) => new {
SiteId = t.SiteId,
SiteAlloc1 = t.SiteAlloc1,
SiteAlloc2 = t.SiteAlloc2,
SiteAlloc3 = t.SiteAlloc3,
SiteAlloc4 = t.SiteAlloc4,
Rank = u + 1));
Where u is the index (0 based, that's why I added 1), or in your case the rank and t is the selected object

How to increment by group

There is a table and now add a new column -- sort_num int default 0
id level sort_num
1 1 0
2 1 0
3 2 0
4 2 0
5 2 0
6 3 0
7 3 0
8 3 0
9 3 0
Now I want to set sort_num values like below
id level sort_num
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 3 1
7 3 2
8 3 3
9 3 4
The Java code implement above requirement is
int sortNum = 0;
int currentLevel = fooList.get(0).getLevel();
for (RuleConf foo : fooList) {
if(currentLevel != foo.getLevel()){
sortNum = 0;
currentLevel = foo.getLevel();
}
foo.setSortNum(++sortNum);
}
I want to know if Java8 could simplify above code?
PS. Use mysql to implement this requirement
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 1 order by id;
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 2 order by id;
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 3 order by id;
The best approach is to stick to your plain enhanced for loop. I don't think it is possible to come up with a single Stream solution, since you need to have intermediate values. Like:
Map<Integer, List<RuleConf>> levels = fooList.stream()
.collect(Collectors.groupingBy(RuleConf::getLevel));
levels.values().forEach(v ->
IntStream.range(0, v.size()).forEach(i -> v.get(i).setSortNum(i + 1))
);
If you keep track of the next order numbers yourself, you may do it with one stream. This solution is thread safe as well, hence should work with parallel streams:
Map<Integer, AtomicInteger> orders = new ConcurrentHashMap<>();
fooList.stream().forEachOrdered(foo -> {
orders.putIfAbsent(foo.getLevel(), new AtomicInteger());
foo.setOrder(orders.get(foo.getLevel()).incrementAndGet());
});
It should outperform the other stream-solutions, because it requires to iterate over the list only ones.

simple join of three tables in LINQ

i dont understand linq properly. i dont know why.
i have three tables.
1)TillTable
tillId, tillName
1 w1
2 w2
3 w3
4 w4
2)TillDepartment
tillDeptId, tillId, deptId, isPart
1 1 5 1
2 1 7 0
3 1 8 0
4 1 9 0
5 2 5 0
6 2 7 0
7 2 8 0
8 2 9 0
9 3 5 0
10 3 7 1
11 3 8 0
12 3 9 0
13 4 5 0
14 4 7 0
15 4 9 0 so on....
3) departmentTable
deptId, deptName
5 Science
7 Commerce
8 history
9 English so on....
now using linq or lambda exp i want to display following result,
tillId, tillName, deptName
1 w1 science
2 w2 no dept
3 w3 commerce
4 w4 no dept so on...
i hope for linq master its damn easy task....
help me to get it resolved....
if isPart column is 1 then in result set deptName should be displayed else 'no dept' ....
associative table has multiple entries for tillId.....
its requirement so strictly follow this scenario only.....
i hope its clr ......
Try this:-
var result = (from tt in db.tillTables
join td in db.tillDepts
on tt.tillId equals td.tillId
join dt in db.departmentTable
on td.deptid equals dt.deptId
select new
{
tillId = tt.tillId,
tillName = tt.tillName,
deptName = td.isPart == 1 ? dt.deptName : null
}).GroupBy(x => x.tillId)
.Select(x =>
{
var orderedDeptRecord = x.OrderByDescending(z => z.deptName).FirstOrDefault();
return new
{
tillId = x.Key,
tillName = orderedDeptRecord.tillName,
deptName = orderedDeptRecord.deptName
};
});
Outer join to single record,then, the foreign key to the deptName is the select clause with ternary operator (gets translated to "case")
from t in TillTable
join tds in TillDepartment on new { t.tillId,isPart=1}
equals new { td.tillId, isPart } into tdOuter
from td in tdOuter.DefaultIfEmpty().Take(1)
select new { t.tillId, t.tillName,
deptName=((td==null)? "no dept" :
(from dt in departmentTable
where dt.deptId == td.deptId
select deptName ) ) }

get the sum of the maxs group by

I have this table
EquipmentId Value Date
1 2 11/04/2013
1 1 11/04/2013
2 3 11/04/2013
2 2 10/04/2013
2 5 10/04/2013
3 1 10/04/2013
3 3 11/04/2013
I want to group these items by date, and have a dictionary with the date as a key and the sum of the maxs of the all equipments values in that day
the result would be like this
[10/04/2013: 6] // 6 = 5 (as the max of values of the the equipmetId 2) + 1 (as the max of values of the the equipmetId 3)
[11/04/2013: 5] // 5 = 2(as the max of values of the the equipmetId 1) + 3(as the max of values of the the equipmetId 3)
I managed to make the query to get this without the sum, meaning for only one equipment.
var consumptionValues = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
join e in context.EquipmentSet on ep.EquipmentID equals e.EquipmentID
where (e.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Now && c.Date >= firstDayDate)
group c by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
dayOfWeek = (DayOfWeek)grp.Key.Value - 1,
value = grp.Max(c => c.Value),
}).ToDictionary(c => c.dayOfWeek.ToString(), c => c.value);
It's the complete query with all the joins, in the example I just gave a simplified example.
Is it possible to do this in one single query ?
I have to say I'm not sure it will work, but you should give it a shot:
var consumptionValues = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
join e in context.EquipmentSet on ep.EquipmentID equals e.EquipmentID
where (e.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Now && c.Date >= firstDayDate)
group new { c, e } by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
dayOfWeek = (DayOfWeek)grp.Key.Value - 1,
value = grp.GroupBy(i => i.e.EquipmentID).Sum(g => g.Max(i => i.c.Value)),
}).ToDictionary(c => c.dayOfWeek.ToString(), c => c.value);

Linq select subgroup

I have the following in my db table:
- MailingId | GroupName | ServiceId
- 1 | group1 | 3
- 2 | group1 | 5
- 3 | group1 | 8
- 4 | group2 | null
- 5 | group3 | null
...
In my view i have 2 groups of checkboxes:
1) (services) with id's 3,5,8 (serviceId).
2) and a list of checkboxes for mailing groups (group1, group2, group3)
I need to select the following using LINQ:
Select rows that I have selected in ServiceId checkbox list PLUS any other. For example if I check off ServiceId's (3 and 5) and group "Group3" then my output would be rows MailingId: 1, 3 and 5. HOWEVER, if I select ANY service from (first group of checkboxes) AND DO NOT select "Group1" from mailing group checkboxes then rows with Group1 SHOULD NOT be in the output.
I'm using EF4. Please help.
Thanks
The 2 arrays would be the selections that are posted from your view
int[] selectedservices = {3,5};
string[] selectedgroups = {"group3"};
using (Model model = new Model())
{
bool b = selectedservices.Contains(1);
var mailinglists = from m in model.MalingSet
where selectedgroups.Contains(m.GroupName)
&& ((m.ServiceId.HasValue && selectedservices.Contains(m.ServiceId.Value)) || m.ServiceId.HasValue == false)
select m.MailingId;
}
Try something like this.
var mailingGroup =
from m in Mailings
where m.ServiceId != null // or whatever other condition
group m by m.GroupName into g
select new
{
groupName = g.Key,
mailings = g
};
It's a Where clause no?
var a = new {m = 1, g = "group1", sid = 3};
var b = new {m = 2, g = "group1", sid = 5};
var c = new {m = 3, g = "group1", sid = 8};
var d = new {m = 4, g = "group2", sid = 0};
var e = new {m = 5, g = "group3", sid = 0};
var l = new List<dynamic>{a,b,c,d,e};
l.Where(it => ( new ArrayList{3, 5}).Contains(it.sid)
|| (new ArrayList{1, 5}).Contains(it.m)).Dump();
http://www.linqpad.net/

Resources