Linq Select First Active Record in a Set - c#

I am trying to return the most recent revision of a document that is not in a cancelled status. However, if all the revision of the document are cancelled, it should still return the most revision in the cancelled status.
Data looks something like:
ID | Name | Rev | Status
00 | Manual | 000 | Active
00 | Manual | 001 | Active
00 | Manual | 002 | Active
00 | Manual | 003 | Active
00 | Manual | 004 | Active //return this one
ID | Name | Rev | Status
01 | Manual2 | 000 | Active
01 | Manual2 | 001 | Active
01 | Manual2 | 002 | Active
01 | Manual2 | 003 | Active //return this one
01 | Manual2 | 004 | Cancel
ID | Name | Rev | Status
02 | Manual3 | 000 | Cancel
02 | Manual3 | 001 | Cancel
02 | Manual3 | 002 | Cancel
02 | Manual3 | 003 | Cancel
02 | Manual3 | 004 | Cancel //return this one
I can group and sort the records easily. I can also filter our records with a cancelled status, but in the case of the 3rd data set, all documents are in cancelled status and I get an exception.
List<Records> r = records
.GroupBy(a => a.ID)
.Select(b => new Record
{
ID = b.Key,
Name = b.First().Name,
Rev = b.OrderByDescending(o => o.Rev)
.First(x=> x.status != "Cancel").Rev
}).ToList();

You're almost there with what you have, you can use ThenBy after an OrderBy to add subsequent ordering:
List<Records> r = records
.GroupBy(a => a.ID)
.Select(b => new Record
{
ID = b.Key,
Name = b.First().Name,
Rev = b.OrderBy(o => o.status == "Cancel")
.ThenByDescending(o => o.Rev)
.First().Rev
}).ToList();
NOTE: In this example I've ordered on status == "Cancel" as false < true.
EDIT
Based on the comments section requirement for extra statuses: you could create a function to convert the status into a numeric rank:
public int GetStatusRank(string status)
{
switch (status)
{
case "active":
return 0;
case "endorsed"
return 1;
case "cancelled":
return 2;
//etc...
default:
return int.MaxValue;
}
}
You could then use this in your OrderBy:
...b.OrderBy(o => GetStatusRank(o.status))

The following Query first orders the Items by Status and Rev. Then the group by picks every ID once. distinctRecord.First() returns the topmost item of that group, hence the ordering.
var query =
from record in records
orderby record.Status ascending, // Active before Cancel ('A' is alphabetically before 'C')
record.Rev descending // highest revision first
group record by record.ID
into distinctRecord
select distinctRecord.First();
var r = query.ToList();
The cool thing about this is, that you won't need to create a new instance of Record. Instead it brings back the actual Object from your Collection.
As you require to create new Instances of Record and not use references to the ones in the collection. You can do It like I've also explained in my comment:
var query =
from record in records
orderby record.Status ascending, // Active before Cancel ('A' is alphabetically before 'C')
record.Rev descending // highest revision first
group record by record.ID
into distinctRecord
select new Record {
ID = distinctRecord.Key,
Name = distinctRecord.First().Name,
Rev = distinctRecord.First().Rev,
Status = distinctRecord.First().Status
};
var r = query.ToList();

try this:
.First(x=> x.status != "Cancel" || x.status == "Cancel")

List<Records> r = records
.OrderByDescending(x => x.Status != "Cancel")
.ThenBy(r => r.Rev).ToList();
after (r=>r.Rev) you can Sorting with anything else what you need. Just use .ThenBy(...)

You can try this:
Create an anonymous object with the Revision object (Active or Cancel)
Create your colllection of Record using the Rev property.
var r = records.GroupBy(x => new {x.Id, x.Name})
.Select(x => new {
ID = x.Key.ID,
Name = x.Key.Name,
RevObj = x.OrderBy(y => y.Rev).LastOrDefault(y => y.Status != "Cancel") ??
x.OrderBy(y => y.Rev).Last(y => y.Status == "Cancel")
})
.ToArray()
.Select(x => new Record() {
ID = x.ID,
Name = x.Name,
Rev = x.RevObj.Rev
})
.ToList();

I would use reducing based on Aggregate-method:
var resul = list
.GroupBy(
k => k.ID,
(key, groups) =>
groups.Aggregate((accumulatedItem, item) =>
{
if (accumulatedItem.Status == item.Status)
{
return string.Compare(accumulatedItem.Rev, item.Rev) >= 0
? accumulatedItem
: item;
}
return accumulatedItem.Status == "Active"
? accumulatedItem
: item;
})
)
.ToArray();
var list = new[] {
new Record("00", "Manual", "000","Active"),
new Record("00", "Manual", "001","Active"),
new Record("00", "Manual", "002","Active"),
new Record("00", "Manual", "003","Active"),
new Record("00", "Manual", "004","Active"),
new Record("01", "Manual2", "000", "Active"),
new Record("01", "Manual2", "001", "Active"),
new Record("01", "Manual2", "002", "Active"),
new Record("01", "Manual2", "003", "Active"),
new Record("01", "Manual2", "004", "Cancel"),
new Record("02", "Manual3", "000", "Cancel"),
new Record("02", "Manual3", "001", "Cancel"),
new Record("02", "Manual3", "002", "Cancel"),
new Record("02", "Manual3", "003", "Cancel"),
new Record("02", "Manual3", "004", "Cancel")
};
public class Record
{
public Record(string id, string name, string rev, string status)
{
ID = id;
Name = name;
Rev = rev;
Status = status;
}
public string ID { get; set; }
public string Name { get; set; }
public string Rev { get; set; }
public string Status { get; set; }
}

Related

Query with join, group by and count to linq

im doing this query on sql and it works, but how do i make it to Linq ?
select b.Name,a.idempresa,count(1) as cuenta from Descarga_XML_recepcion_V2 as a
inner join EnterpriseSGE as b on a.idempresa = b.EnterpriseSGEId group by idempresa,b.name
this is what it should bring
name1 | 5041 | 583
name2 | 4031 | 1730
name3 | 5042 | 640
name4 | 4034 | 300
name5 | 6047 | 861
name6 | 5043 | 187
name7 | 4033 | 318
A straight forward translation of the SQL into LINQ yields:
var ans = from a in Descarga_XML_recepcion_V2
join b in EnterpriseSGE on a.idempresa equals b.EnterpriseSGEId
group 1 by new { a.idempresa, b.name } into ingroup
select new {
ingroup.Key.idempresa,
ingroup.Key.name,
cuenta = ingroup.Count()
};
Try :
var results = (from a in Descarga_XML_recepcion_V2
join b in EnterpriseSGE on a.idempresa equal b.EnterpriseSGEId
select new { a = a, b = b})
.GroupBy(x => new { idempresa = x.a.idempresa, name = x.b.name})
.Select(x => new {name = x.Key.name, idempresa = x.Key.idempressa, count = x.Count()})
.ToList();

Converting SQL to Linq query

I'm trying to get the output of the following query into a Linq query
SELECT SearchQueries.Query,
Clicks.Name,
COUNT (SearchQueries.Query) AS Hits
FROM SearchQueries
INNER JOIN Clicks ON Clicks.SearchqueryId = SearchQueries.Id
GROUP BY SearchQueries.Query, Clicks.Name
ORDER BY Hits DESC
But I can't seem to figure out how to do this;
this is what I have so far
var result =
_db.Clicks.Select(q => q)
.GroupBy(q => q.Name, g => g.Searchquery.Query)
.ToDictionary(g=> g.Key, g => g);
but how would I continue?
the result is something like this:
+---------------+-------------------+------+
|Query | Name | Hits |
+---------------+-------------------+------+
|tag | dnfsklmfnsd | 53 |
|tag2 | dgsqfsdf | 17 |
+---------------+-------------------+------+
The original tables looks like following
SearchQueries;
+---+-------+
|Id | Query |
+---+-------+
| 1 | tag | x 53
| 2 | tag2 | x 17
+---+-------+
Clicks;
+---+-------------------+---------------+
|Id | Name | SearchqueryId |
+---+-------------------+---------------+
| 1 | dnfsklmfnsd | 1 |
| 2 | dgsqfsdf | 2 |
+---+-------------------+---------------+
Try to use GroupBy and Count: (I changed the order to using SearchQueries as "base table" in the expression, just to make it more easy to compare to the SQL-statement)
var result =
_db.SearchQueries
.GroupBy(sq => new { name = sq.Clicks.Name, query = sq.Query)
.Select(sq => new {
Query = sq.Query,
Name = sq.Clicks.Name,
Hits = sq.Count()
})
.OrderByDescending(sq => sq.Hits);
Well, if you have a navigation property Searchquery on Click, as it looks like, you can do
var result =
_db.Clicks
.GroupBy(m => new {name = m.Name, query = m.Searchquery.Query)
.Select(g => new {
Query = g.Key.query,
Name = g.Key.name,
Hits = g.Count()
});

C# linq query from sql server into one table

I have three tables. I would like to used C# linq to turn into one table.
For example:
Schedule:
+------+--------+--------+
| Name | DateId | TaskId |
+------+--------+--------+
| John | 2 | 32 |
| John | 3 | 31 |
| Mary | 1 | 33 |
| Mary | 2 | 31 |
| Tom | 1 | 34 |
| Tom | 2 | 31 |
| Tom | 3 | 33 |
+------+--------+--------+
Date:
+----+------------+
| Id | Date |
+----+------------+
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
+----+------------+
Task:
+----+----------+
| Id | Task |
+----+----------+
| 31 | School |
| 32 | Homework |
| 33 | Break |
| 34 | Teaching |
+----+----------+
I would like to have a table like this:
+--------+----------+----------+-----------+----------+
| Person | Monday | Tuesday | Wednesday | Thursday |
+--------+----------+----------+-----------+----------+
| John | | Homework | School | |
| Mary | Break | School | | |
| Tom | Teaching | School | Break | |
+--------+----------+----------+-----------+----------+
I could not think of any good way doing this.
Any suggestion would be helpful
Thanks
Starting from this set of data:
var schedules = new[] { new { Name = "John", DateId = 2, TaskId = 32},
new { Name = "John", DateId = 3, TaskId = 31},
new { Name = "Mary", DateId = 1, TaskId = 33},
new { Name = "Mary", DateId = 2, TaskId = 31},
new { Name = "Tom", DateId = 1, TaskId = 34},
new { Name = "Tom", DateId = 2, TaskId = 31},
new { Name = "Tom", DateId = 3, TaskId = 33}
};
var dates = new[] { new { DateId = 1, Desc = "Monday"},
new { DateId = 2, Desc = "Tuesday"},
new { DateId = 3, Desc = "Wednesday"},
new { DateId = 4, Desc = "Thursday"},
new { DateId = 5, Desc = "Friday"}
};
var tasks = new[] { new { TaskId = 31, Desc = "School"},
new { TaskId = 32, Desc = "Homework"},
new { TaskId = 33, Desc = "Break"},
new { TaskId = 34, Desc = "Teaching"}
};
You can do as follows:
var result = schedules
// First you join the three tables
.Join(dates, s => s.DateId, d => d.DateId, (s, d) => new {s, d})
.Join(tasks, s => s.s.TaskId, t => t.TaskId, (sd, t ) => new { Person = sd.s, Date = sd.d, Task = t })
// Then you Group by the person name
.GroupBy(j => j.Person.Name)
// Finally you compose the final object extracting from the list of task the correct task for the current day
.Select(group => new
{
Person = group.Key,
Monday = group.Where(g => g.Date.DateId == 1).Select(g => g.Task.Desc).FirstOrDefault(),
Tuesday = group.Where(g => g.Date.DateId == 2).Select(g => g.Task.Desc).FirstOrDefault(),
Wednesday = group.Where(g => g.Date.DateId == 3).Select(g => g.Task.Desc).FirstOrDefault(),
Thursday = group.Where(g => g.Date.DateId == 4).Select(g => g.Task.Desc).FirstOrDefault(),
Friday = group.Where(g => g.Date.DateId == 5).Select(g => g.Task.Desc).FirstOrDefault()
})
.ToList();
If you want to select only some days, you can return an object containing a dictionary instead of an object with a property per day.
The dictionary will contain key-value pairs with the key representing the day and the value representing the task.
See the following code:
var filter = new[] {2, 3};
var filteredResult = schedules
.Join(dates, s => s.DateId, d => d.DateId, (s, d) => new{ s, d})
.Join(tasks, s => s.s.TaskId, t => t.TaskId, (sd, t) => new { Person = sd.s, Date = sd.d, Task = t })
.Where(x => filter.Contains(x.Date.DateId))
.GroupBy(x => x.Person.Name)
.Select(group => new
{
Person = group.Key,
TasksByDay = group.ToDictionary(o => o.Date.Desc, o => o.Task.Desc)
})
.ToList();
foreach (var item in filteredResult)
{
System.Console.WriteLine(item.Person);
foreach (var keyvaluepair in item.TasksByDay)
{
System.Console.WriteLine(keyvaluepair.Key + " - " + keyvaluepair.Value);
}
System.Console.WriteLine("---");
}
It is called "transpose".
var persons = new[] { new { name="John", dateId=2,taskId=32},
new { name="John", dateId=3,taskId=31},
new { name="Mary", dateId=1,taskId=33},
new { name="Mary", dateId=2,taskId=31},
new { name="Tom", dateId=1,taskId=34},
new { name="Tom", dateId=2,taskId=31},
new { name="Tom", dateId=3,taskId=33}
};
var dates = new[] { new { dateId=1, desc="Monday"},
new { dateId=2, desc="Tuesday"},
new { dateId=3, desc="Wednesday"},
new { dateId=4, desc="Thursday"},
new { dateId=5, desc="Friday"}
};
var tasks = new[] { new { taskId=31, desc="School"},
new { taskId=32, desc="Homework"},
new { taskId=33, desc="Break"},
new { taskId=34, desc="Teaching"}
};
var qry = from p in (from p in persons
join d in dates on p.dateId equals d.dateId
join t in tasks on (int)p.taskId equals (int)t.taskId
select new { name = p.name, monday = d.dateId == 1 ? t.desc : "", tuesday = d.dateId == 2 ? t.desc : "", wednesday = d.dateId == 3 ? t.desc : "", thursday = d.dateId == 4 ? t.desc : "", friday = d.dateId == 5 ? t.desc : "" })
group p by p.name into q
select new { q.Key, monday=q.Max(a => a.monday),tuesday=q.Max(a => a.tuesday), wednesday = q.Max(a=>a.wednesday), thursday = q.Max(a => a.thursday), friday=q.Max(a => a.friday)};
foreach ( var a in qry.ToList())
{
Console.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",a.Key, a.monday, a.tuesday, a.wednesday, a.thursday, a.friday));
}

Select all columns but group by only one in linq

I have been looking for a way to get multiple columns but group by only one in SQL and I found some info. However I can not came up with a way to do it in linq.
I have the following toy example table:
| Id | Message | GroupId | Date |
|-------------------------------|
| 1 | Hello | 1 | 1:00 |
| 2 | Hello | 1 | 1:01 |
| 3 | Hey | 2 | 2:00 |
| 4 | Dude | 3 | 3:00 |
| 5 | Dude | 3 | 3:01 |
And I would like to recover all columns for the rows that have a distinct GroupId as follows (with a 'Date' desc order):
| Id | Message | GroupId | Date |
|-------------------------------|
| 1 | Hello | 1 | 1:00 |
| 3 | Hey | 2 | 2:00 |
| 4 | Dude | 3 | 3:00 |
I do not really care about which row is picked from the grouped ones (first, second...) as long as is the only one given that group Id.
I have came out with the following code so far but it does not do what is supposed to:
List<XXX> messages = <MyRep>.Get(<MyWhere>)
.GroupBy(x => x.GroupId)
.Select(grp => grp.OrderBy(x => x.Date))
.OrderBy(y => y.First().Date)
.SelectMany(y => y).ToList();
This will give you one item per group:
List<dynamic> data = new List<dynamic>
{
new {ID = 1, Message = "Hello", GroupId = 1, Date = DateTime.Now},
new {ID = 2, Message = "Hello", GroupId = 1, Date = DateTime.Now},
new {ID = 3, Message = "Hey", GroupId = 2, Date = DateTime.Now},
new {ID = 4, Message = "Dude", GroupId = 3, Date = DateTime.Now},
new {ID = 5, Message = "Dude", GroupId = 3, Date = DateTime.Now},
};
var result = data.GroupBy(item => item.GroupId)
.Select(grouping => grouping.FirstOrDefault())
.OrderByDescending(item => item.Date)
.ToList();
//Or you can also do like this:
var result = data.GroupBy(item => item.GroupId)
.SelectMany(grouping => grouping.Take(1))
.OrderByDescending(item => item.Date)
.ToList();
If you want to control OrderBy then:
var result = data.GroupBy(item => item.GroupId)
.SelectMany(grouping => grouping.OrderBy(item => item.Date).Take(1))
.OrderByDescending(item => item.Date)
.ToList();

Combine tables using row values as column LINQ C# SQL

I have a users table:
Id | Name | Age
--------------------
1 | Steve | 21
2 | Jack | 17
3 | Alice | 25
4 | Harry | 14
I also have a table containing additional user info:
UId | Key | Value
----------------------
1 | Height | 70
2 | Height | 65
2 | Eyes | Blue
4 | Height | 51
3 | Hair | Brown
1 | Eyes | Green
The UId column links to the Id column in the users table. As you can see, not all users have the same additional info present. Alice doesn't have a height value, Jack is the only one with an eye color value etc.
Is there a way to combine this data into one table dynamically using C# and LINQ queries so that the result is something like this:
Id | Name | Age | Height | Eyes | Hair
------------------------------------------
1 | Steve | 21 | 70 | Green |
2 | Jack | 17 | 65 | Blue |
3 | Alice | 25 | | | Brown
4 | Harry | 14 | 51 |
If a user does not have a value for the column, it can remain empty/null. Does this require some sort of data pivoting?
For the case, your user info fields are constant:
var result = users.GroupJoin(details,
user => user.Id,
detail => detail.Id,
(user, detail) => new
{
user.Id,
user.Name,
user.Age,
Height = detail.SingleOrDefault(x => x.Key == "Height").Value,
Eyes = detail.SingleOrDefault(x => x.Key == "Eyes").Value,
Hair = detail.SingleOrDefault(x => x.Key == "Hair").Value,
});
You can do it by utilising GroupJoin, example:
var users = new List<Tuple<int, string, int>> {
Tuple.Create(1, "Steve", 21),
Tuple.Create(2, "Jack", 17),
Tuple.Create(3, "Alice", 25),
Tuple.Create(4, "Harry", 14)
};
var userInfos = new List<Tuple<int, string, string>> {
Tuple.Create(1, "Height", "70"),
Tuple.Create(2, "Height", "65"),
Tuple.Create(2, "Eyes", "Blue"),
Tuple.Create(4, "Height", "51"),
Tuple.Create(3, "Hair", "Brown"),
Tuple.Create(1, "Eyes", "Green"),
};
var query = users.GroupJoin(userInfos,
u => u.Item1,
ui => ui.Item1,
(u, infos) => new { User = u, Infos = infos });
var result = query.Select(qi => new
{
Id = qi.User.Item1,
Name = qi.User.Item2,
Age = qi.User.Item3,
Height = qi.Infos.Where(i => i.Item2 == "Height").Select(i => i.Item3).SingleOrDefault(),
Eyes = qi.Infos.Where(i => i.Item2 == "Eyes").Select(i => i.Item3).SingleOrDefault(),
Hair = qi.Infos.Where(i => i.Item2 == "Hair").Select(i => i.Item3).SingleOrDefault()
});
First of all I have grouped the user details data using Feature (I have renamed the Key property with Feature to avoid confusion) & UId then I have used group join to combine both results using into g. Finally retrieved the result using specified feature.
var result = from user in users
join detail in details.GroupBy(x => new { x.UId, x.Feature })
on user.Id equals detail.Key.UId into g
select new
{
Id = user.Id,
Name = user.Name,
Age = user.Age,
Height = g.FirstOrDefault(z => z.Key.Feature == "Height") != null ?
g.First(z => z.Key.Feature == "Height").First().Value : String.Empty,
Eyes = g.FirstOrDefault(z => z.Key.Feature == "Eyes") != null ?
g.First(z => z.Key.Feature == "Eyes").First().Value : String.Empty,
Hair = g.FirstOrDefault(z => z.Key.Feature == "Hair") != null ?
g.First(z => z.Key.Feature == "Hair").First().Value : String.Empty,
};
I am getting following output:-
Here is the complete Working Fiddle.
Try this
var list = (from u in context.users
join ud in context.UserDetails on u.Id equals ud.UId
select new
{
u.Id,
u.Name,
u.Age,
ud.Key,
ud.Value
});
var finallist = list.GroupBy(x => new { x.Id, x.Name,x.Age}).Select(x => new
{
x.Key.Id,
x.Key.Name,
x.Key.Age,
Height = x.Where(y => y.Key == "Height").Select(y => y.Value).FirstOrDefault(),
Eyes = x.Where(y => y.Key == "Eyes").Select(y => y.Value).FirstOrDefault(),
Hair = x.Where(y => y.Key == "Hair").Select(y => y.Value).FirstOrDefault()
}).ToList();
try this query
var objlist=( form a in contex.user
join b in contex.UserDetails on a.id equals a.Uid into gj
from subpet in gj.DefaultIfEmpty()
select new { Id=a.id, Name=a.name, Age=a.age, Height =subpet.Height,Eyes=subpet.Eyes, Hair=subpet.Hair}).ToList();

Categories

Resources