Given
(1) A database table, stored as a list of lists. The size of the table in terms of rows and columns is undefined.
List<List<string>> table = new List<List<string>>();
For example:
table.Add(new List<string>() { "a1", "b1", "c1", "d1", "e1" });
table.Add(new List<string>() { "a2", "b2", "c2", "d2", "e2" });
table.Add(new List<string>() { "a3", "b3", "c3", "d3", "e3" });
| a1 | b1 | c1 | d1 | e1 |
| a2 | b2 | c2 | d2 | e2 |
| a3 | b3 | c3 | d3 | e3 |
(2) A list of integers. These integers resemble the indexes of database columns (zero-based), e.g.:
List<int> indexes = new List<int>() { 1, 3, 4 };
Problem
My aim is to project those columns from table of which the indexes occur in the list indexes. Given the above examples, the result should be:
| b1 | d1 | e1 |
| b2 | d2 | e2 |
| b3 | d3 | e3 |
Current Solution
The best I could come up with is to iterate over all rows, like this:
List<List<string>> subtable = new List<List<string>>();
for (int index = 0; index < table.Count; index++)
{
subtable.Add(table[index].Where((t, i) => indexes.Contains(i)).ToList());
}
Request
A more elegant solution, if possible.
What about this:
List<List<string>> subtable =
table.Select(row => indexes.Select(i => row[i]).ToList()).ToList();
In case you need to check the array bounds, you can do this:
List<List<string>> subtable =
table.Select(row => indexes.Where(i => i >= 0 && i < row.Count)
.Select(i => row[i]).ToList()).ToList();
Or if you prefer query syntax:
List<List<string>> subtable =
(from row in table
select
(from i in indexes
where i >= 0 && i < row.Count
select row[i]
).ToList()
).ToList();
Select all the rows, then for each row filter out the columns that are not in your index list:
var subtable = table
.Select(row => row.Where((value, colIndex) => indexes.Contains(colIndex)))
.ToList();
If you want just to print, it is simpler (and more efficient ) to do it without queries like this:
List<List<string>> table = new List<List<string>>();
table.Add(new List<string>() { "a1", "b1", "c1", "d1", "e1" });
table.Add(new List<string>() { "a2", "b2", "c2", "d2", "e2" });
table.Add(new List<string>() { "a3", "b3", "c3", "d3", "e3" });
List<int> indexes = new List<int>() { 1, 3, 4 };
for (int index = 0; index < table.Count; index++)
{
foreach (var columnIndex in indexes)
Console.Write(table[index][columnIndex] +" ");
Console.WriteLine();
}
Console.ReadLine();
Related
I have two DataTables: dt1 and dt2.
dt1:
ID | Name | Address | QTY
-------+----------+---------+-----
A1 | Dog | C1 | 272
A2 | Cat | C3 | 235
A3 | Chicken | C2 | 254
A4 | Mouse | C4 | 259
A5 | Pig | C5 | 233
dt2:
ID | Name | Address | QTY MAX
-------+----------+---------+--------
A1 | Dog | C1 | 250
A2 | Cat | C3 | 200
A3 | Chicken | C2 | 300
A6 | Rabbit | C6 | 350
But, I want to merge dt1 and dt2 to dt3 like below:
ID | Name | Address | QTY | QTY MAX
-------+----------+---------+-------+--------
A1 | Dog | C1 | 272 | 250
A2 | Cat | C3 | 235 | 200
A3 | Chicken | C2 | 254 | 300
A4 | Mouse | C4 | 259 | 0
A5 | Pig | C5 | 233 | 0
A6 | Rabbit | C6 | 0 | 350
Can any one help me?
If your DataTables haven't primary key and you can't or don't want to change those DataTables you can use a code like this:
// At first you need to define your result `DataTable`
// So make it by cloning from first `DataTable`
var dt3 = dt1.Clone();
// Then add extra columns to it
dt3.Columns.Add("Qty Max", typeof(int));
// Second, you need to add rows of first `DataTable`
foreach (DataRow row in dt1.Rows)
{
// When you don't have a primary key you need a code like this to find same rows:
var dt2Row = dt2.Rows.OfType<DataRow>().SingleOrDefault(w => w["ID"].Equals(row["ID"]));
var qtyMax = dt2Row?["Qty Max"] ?? 0; // Here I set default value to `0`
dt3.Rows.Add(row["ID"], row["Name"], row["Address"], row["Qty"], qtyMax);
}
// Third, you need to add rows of second `DataTable` that is not in first
var dt2OnlyRows =
dt2.Rows.OfType<DataRow>().Where(w => dt1.Rows.OfType<DataRow>().All(x => x["ID"] != w["ID"]));
foreach (var row in dt2OnlyRows)
{
dt3.Rows.Add(row["ID"], row["Name"], row["Address"], 0, row["Qty Max"]);
}
This solution is not a linq solution as you could simply use DataTable.Merge & DataTable.PrimaryKey to get the desired output.
Here is a dummy example which you can use:
var dt1 = new DataTable();
var p1 = dt1.Columns.Add("a", typeof(int)); //Use this to add Primary Key constraint
dt1.Columns.Add("b");
dt1.Columns.Add("c");
dt1.Rows.Add("1", "apple", "10");
dt1.Rows.Add("2", "mango", "20");
dt1.Rows.Add("3", "orange", "30");
dt1.Rows.Add("4", "banana", "40");
dt1.PrimaryKey = new DataColumn[] { p1 }; //This removes duplication of rows
var dt2 = new DataTable();
var p2 = dt2.Columns.Add("a", typeof(int)); //Use this to add Primary Key constraint
dt2.Columns.Add("b");
dt2.Columns.Add("d");
dt2.Rows.Add("1", "apple", "50");
dt2.Rows.Add("2", "mango", "60");
dt2.Rows.Add("3", "orange", "70");
dt2.Rows.Add("5", "grapes", "80");
dt2.PrimaryKey = new DataColumn[] { p2 }; //This removes duplication of rows
var dt3 = dt1.Copy();
dt3.Merge(dt2); // Merge here merges the values from both provided DataTables
Taking your question into consideration:
var dt1 = new DataTable();
var p1 = dt1.Columns.Add("ID", typeof(string));
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Address", typeof(string));
dt1.Columns.Add("Qty", typeof(int));
dt1.Columns["Qty"].DefaultValue = 0; //Setting default value
dt1.Rows.Add("A1", "Dog", "C1", 100);
dt1.Rows.Add("A2", "Cat", "C3", 200);
dt1.Rows.Add("A3", "Chicken", "C2", 300);
dt1.Rows.Add("A4", "Mouse", "C4", 400);
dt1.Rows.Add("A5", "Pig", "C5", 500);
dt1.PrimaryKey = new DataColumn[] { p1 };
var dt2 = new DataTable();
var p2 = dt2.Columns.Add("ID", typeof(string));
dt2.Columns.Add("Name", typeof(string));
dt2.Columns.Add("Address", typeof(string));
dt2.Columns.Add("Qty Max", typeof(int));
dt2.Columns["Qty Max"].DefaultValue = 0; //Setting default value
dt2.Rows.Add("A1", "Dog", "C1", 600);
dt2.Rows.Add("A2", "Cat", "C3", 700);
dt2.Rows.Add("A3", "Chicken", "C2", 800);
dt2.Rows.Add("A6", "Rabbit", "C6", 900);
dt2.PrimaryKey = new DataColumn[] { p2 };
var dt3 = dt1.Copy();
dt3.Merge(dt2);
Output:
Thanks #shA.t for suggesting to include DataColumn.DefaultValue so that blank cells could be replaced with 0. Also his answer seems to include linq features which I guess is what you are looking for!
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();
I have table with two columns:
+-------------+------------+
| Level | Desc |
+-------------+------------+
| 1 | a |
+-------------+------------+
| 2 | b |
+-------------+------------+
| 2 | c |
+-------------+------------+
| 1 | d |
+-------------+------------+
| 2 | e |
+-------------+------------+
| 2 | f |
+-------------+------------+
| 3 | g |
+-------------+------------+
| 1 | h |
+-------------+------------+
| 1 | i |
+-------------+------------+
| 2 | j |
+-------------+------------+
| 2 | k |
+-------------+------------+
And I need to create display of this data in XtraTreeview with two columns according to Level column and it should be like:
- 1 a
-- 2 b
-- 2 c
-1 d
-- 2 e
-- 2 f
-- 3 g
-1 h
-1 i
-- 2 j
-- 2 k
So, level columns represents the node. Level 1 is the main node, level 2 is subnode of level 1, level 3 is subnode of level 2, level 4 is subnode of 3...
I know how to populate Xtratreeview when there is fixed numbers of nodes and subnodes but in this case don't have idea how to populate where 1 node consist 3, 4 or more subnodes.
I've done this so far:
Populate TreeView:
DataTable table = new DataTable();
table.Columns.Add("Level");
table.Columns.Add("Data");
table.Rows.Add(1, "a");
table.Rows.Add(2, "b");
table.Rows.Add(2, "c");
table.Rows.Add(1, "d");
table.Rows.Add(2, "e");
table.Rows.Add(2, "f");
table.Rows.Add(3, "g");
table.Rows.Add(4, "z");
table.Rows.Add(5, "x");
table.Rows.Add(2, "h");
table.Rows.Add(3, "i");
table.Rows.Add(1, "j");
table.Rows.Add(2, "k");
TreeListNode rootNode = null;
for (int i = 0; i < table.Rows.Count; i++)
{
tl.BeginUnboundLoad();
TreeListNode parentForRootNodes = null;
if (table.Rows[i][0].ToString().Equals("1"))
{
rootNode = tl.AppendNode(new object[] { (string)table.Rows[i][1] }, parentForRootNodes);
}
if (table.Rows[i][0].ToString().Equals("2"))
{
tl.AppendNode(new object[] { (string)table.Rows[i][1] }, rootNode);
}
tl.EndUnboundLoad();
}
Create columns:
private void CreateColumns2(TreeList tl)
{
tl.BeginUpdate();
tl.Columns.Add();
tl.Columns[0].Caption = "Level";
tl.Columns[0].VisibleIndex = 0;
tl.Columns.Add();
tl.Columns[1].Caption = "Desc";
tl.Columns[1].VisibleIndex = 1;
tl.Columns.Add();
tl.EndUpdate();
}
Documentation you might like to read is here: https://documentation.devexpress.com/#windowsforms/CustomDocument198
You need at minimum three things for a tree:
Id
ParentId
Text
So the structure you've described needs to change to permit finding the parent for an item.
Once you have that, the concept goes like this:
Create an item for each node you want in the tree, I created my own class for this with the properties I wanted (Id, ParentId, Text...)
Then set the datasource of the tree control
Example:
var data = new List<TreeItem>
{
new TreeItem { Id = "L1_1", ParentId = "", Text = "ONE" },
new TreeItem { Id = "L1_2", ParentId = "", Text = "TWO" },
new TreeItem { Id = "L1_3", ParentId = "", Text = "THREE" },
new TreeItem { Id = "L2_1", ParentId = "L1_1", Text = "A" },
new TreeItem { Id = "L2_2", ParentId = "L1_1", Text = "B" },
new TreeItem { Id = "L2_3", ParentId = "L1_2", Text = "C" },
new TreeItem { Id = "L2_4", ParentId = "L1_2", Text = "D" },
new TreeItem { Id = "L2_5", ParentId = "L1_2", Text = "E" }
};
tree.Properties.DataSource = data;
}
class TreeItem
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Text { get; set; }
}
The order of the items in the data source is irrelevant, what is important is the uniqueness of each id.
The above example produces a tree like this:
- ONE
-- A
-- B
- TWO
- THREE
-- C
-- D
-- E
I am doing this without my DevExpress installation and without a compiler, so please excuse any errors. however the concept remains the same.
I have an IDictionary<string,int> containing a distinct list of categories and respective counts with data looking something like this:
Category | Count
-------------------
A | 2
B | 2
Z | 2
A_B | 1
A_B_C | 5
I'm looking for a query that sums the count of categories that start with common letters/string, so from the above the results I'm looking for is as follows:
Category | Count
-------------------
A | 8 <- (a sum of A, A_B, A_B_C)
B | 2
Z | 2
A_B | 6 <- (a sum of A_B, A_B_C)
A_B_C | 5
var d = new Dictionary<string, int>()
{
{ "A", 2 },
{ "B", 2 },
{ "Z", 2 },
{ "A_B", 1 },
{ "A_B_C", 5 },
};
var d2 = d.Select(kvp => new KeyValuePair<string, int>(
kvp.Key,
d.Where(k => k.Key.StartsWith(kvp.Key))
.Sum(k => k.Value)))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, int> data = new Dictionary<string,int>();
data.Add("A", 2);
data.Add("B", 2);
data.Add("Z", 2);
data.Add("A_B", 1);
data.Add("A_B_C", 5);
data.Dump();
var result = from d in data
select new {Key = d.Key, Sum = (from dt in data where dt.Key.StartsWith(d.Key) select dt).Sum(x=>x.Value)};
result.Dump();
Linqpad code
say I've got a DataTable in this format:
id | key1 | key2 | data1 | data2 | parentID
10 | AA | one | 10.3 | 0.3 | -1
10 | AA | two | 20.1 | 16.2 | -1
10 | BB | one | -5.9 | 30.1 | -1
20 | AA | one | 403.1 | -20.4 | 10
30 | AA | one | 121.5 | 210.3 | -1
and a second DataTable like so:
id | data
10 | 5500
20 | -3000
30 | 500
what I want to do is aggregate the data at the "id" level, with the second table's "data" field added to the first's net "data1", and "data2" just summed up by itself. I figured out how to do this, but what I'm stuck at is this: I want data for anything with "parentID" != -1 to be added to it's parent. so the output of the above data should be
id | data1 | data2
10 | 2927.6 | 26.2
30 | 621.5 | 210.3
is there an efficient way to do this?
edit: code sample
DataTable dt1 = new DataTable();
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("key1", typeof(string));
dt1.Columns.Add("key2", typeof(string));
dt1.Columns.Add("data1", typeof(double));
dt1.Columns.Add("data2", typeof(double));
dt1.Columns.Add("parentID", typeof(int));
DataTable dt2 = new DataTable();
dt2.Columns.Add("id", typeof(int));
dt2.Columns.Add("data", typeof(double));
dt1.Rows.Add(new object[] { 10, "AA", "one", 10.3, 0.3, -1 });
dt1.Rows.Add(new object[] { 10, "AA", "two", 20.1, 16.2, -1 });
dt1.Rows.Add(new object[] { 10, "BB", "one", -5.9, 30.1, -1 });
dt1.Rows.Add(new object[] { 20, "AA", "one", 403.1, -20.4, 10 });
dt1.Rows.Add(new object[] { 30, "AA", "one", 121.5, 210.3, -1 });
dt2.Rows.Add(new object[] { 10, 5500 });
dt2.Rows.Add(new object[] { 20, -3000 });
dt2.Rows.Add(new object[] { 30, 500 });
var groups = dt1.AsEnumerable()
.GroupBy(e => e["id"])
.Select(e => new
{
id = e.Key,
net_data1 = e.Sum(w => (double)w["data1"]),
net_data2 = e.Sum(w => (double)w["data2"])
})
.GroupJoin(dt2.AsEnumerable(), e1 => e1.id, e2 => e2["id"],
(a1, a2) => new
{
id = a1.id,
net_data1 = a1.net_data1 + a2.Sum(w => (double)w["data"]),
net_data2 = a1.net_data2
});
Unfortunately, SQL (and, by extension, LINQ) is not well-suited to recursion. Can the parentID column go multiple levels deep? Like this:
ID Parent
------------------
10 -1
20 10
30 10
40 20
If you want to retrace the steps up from ID 40 to ID 10, then you should abandon a SQL/LINQ approach and just do it in code.
It sounds like a good use of a group join. Something like this might work (though it's completely untested):
var items = from parent in context.dataTable
join child in context.dataTable on parent.id equals child.parentID into children
where parent.parentID == -1
select new { id = parent.id,
data1 = (parent.data1 + children.Sum(c => c.data1)),
data2 = (parent.data2 + children.Sum(c => c.data2)) };