How to extract particular columns values from multiple rows using linq - c#

I have a model class like this.
Public Class SampleModel
{
public string ItemName{ get; set; }
public string ItemNo{ get; set; }
public int ItemQty{ get; set; }
public string GroupName{ get; set; }
public int Group1 { get; set; }
public int Group2 { get; set; }
public int Group3 { get; set; }
public int Group4 { get; set; }
}
The Table Contains the values for first 4 ItemName,ItemNo,ItemQty and GroupName.
for each ItemNo are having 4 rows and only ItemQty will be differ which is entered by each groupName(Group1Count ,Group2Count ,Group3Count ,GroupCount ).
Table content like this .
ItemName ItemNo ItemQty GroupName
Pen 234 2 Group1
Pen 234 4 Group2
Pen 234 6 Group3
Pen 234 3 Group4
item2 365 3 Group1
item2 365 5 Group2
item2 365 2 Group3
item2 365 3 Group4
item3 370 3 Group1
item3 370 2 Group4
item4 372 6 Group2
item4 372 9 Group4
so i am getting all the values using this query.
var data= from a in context.Batch where a.GroupName != "" select a;
from this again i need filter out based on the combination of ItemName,ItemNo,ItemQty and show the result like this.
ItemName ItemNo Group1 Group2 Group3 Group4
Pen 234 2 4 6 3
item2 365 3 5 2 3
item3 370 3 0 0 2
item4 372 0 6 0 9
Please suggest me how to write a linq query for this.

You can use
var list=context.Batch.GroupBy(x=>new{x.ItemName,x.ItemNo})
.Select(x=>new
{
ItemName=x.Key.ItemName,
ItemNo=x.Key.ItemNo,
Group1=x.Where(y=>y.GroupName=="Group1").FirstOrDefault(),
Group2=x.Where(y=>y.GroupName=="Group2").FirstOrDefault(),
Group3=x.Where(y=>y.GroupName=="Group3").FirstOrDefault(),
Group4=x.Where(y=>y.GroupName=="Group4").FirstOrDefault()
})
.Select(x=> new SampleModel{
ItemName=x.ItemName,
ItemNo=x.ItemNo,
Group1=x.Group1==null?0:x.Group1.ItemQty,
Group2=x.Group2==null?0:x.Group2.ItemQty,
Group3=x.Group3==null?0:x.Group3.ItemQty,
Group4=x.Group4==null?0:x.Group4.ItemQty
}).ToList();

var data = context.Batch
.GroupBy(i => new {i.ItemName, i.ItemNo})
.Select(g => new {
ItemName = g.Key.ItemName,
ItemNo = g.Key.ItemNo,
Group1 = g.Where(i => i.GroupName == “Group1”).First().ItemQty,
Group2 = g.Where(i => i.GroupName == “Group2”).First().ItemQty,
Group3 = g.Where(i => i.GroupName == “Group3”).First().ItemQty,
Group4 = g.Where(i => i.GroupName == “Group4”).First().ItemQty
})
.Where(i => i.GroupName != “”);

Here is the correct solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ItemName", typeof(string));
dt.Columns.Add("ItemNo", typeof(int));
dt.Columns.Add("ItemQty", typeof(int));
dt.Columns.Add("GroupName", typeof(string));
dt.Rows.Add(new object[] {"Pen",234, 2, "Group1"});
dt.Rows.Add(new object[] {"Pen",234, 4, "Group2"});
dt.Rows.Add(new object[] {"Pen",234, 6, "Group3"});
dt.Rows.Add(new object[] {"Pen",234, 3, "Group4"});
dt.Rows.Add(new object[] {"item2",365, 3, "Group1"});
dt.Rows.Add(new object[] {"item2",365, 5, "Group2"});
dt.Rows.Add(new object[] {"item2",365, 2, "Group3"});
dt.Rows.Add(new object[] {"item2",365, 3, "Group4"});
List<string> groupNames = dt.AsEnumerable().Select(x => x.Field<string>("GroupName")).Distinct().ToList();
DataTable pivot = new DataTable();
pivot.Columns.Add("ItemName", typeof(string));
pivot.Columns.Add("ItemNo", typeof(int));
foreach (string groupName in groupNames)
{
pivot.Columns.Add(groupName, typeof(string));
}
var items = dt.AsEnumerable().GroupBy(x => x.Field<string>("ItemName"));
foreach (var item in items)
{
DataRow pivotRow = pivot.Rows.Add();
pivotRow["ItemName"] = item.First().Field<string>("ItemName");
pivotRow["ItemNo"] = item.First().Field<int>("ItemNo");
foreach (var group in item)
{
pivotRow[group.Field<string>("GroupName")] = group.Field<int>("ItemQty");
}
}
}
}
}

Related

c# LINQ query from master and masterdetail table

I have two tables.
One is Master
Id
Date
1
2022-03-12
2
2022-02-14
3
2021-10-15
4
2021-04-09
5
2020-06-24
Another one is Detail
Id
MasterId
Name
Quantity
1
1
item1
25
2
1
item2
30
3
1
item3
20
4
2
item1
25
5
2
item2
20
6
3
item1
35
7
4
item4
25
8
5
item1
25
9
5
item3
29
From above two table I need a query which will give me 3rd table
Year
item1
item2
item3
item4
2020
25
0
29
0
2021
35
0
0
25
2022
50
50
20
0
You want a pivot table
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication18
{
class Program
{
public static void Main(String[] args)
{
DataTable masterTable = new DataTable("Master");
masterTable.Columns.Add("Id", typeof(int));
masterTable.Columns.Add("Date", typeof(DateTime));
masterTable.Rows.Add(new object[] { 1, DateTime.Parse("2022-03-12") });
masterTable.Rows.Add(new object[] { 2, DateTime.Parse("2022-02-14") });
masterTable.Rows.Add(new object[] { 3, DateTime.Parse("2021-10-15") });
masterTable.Rows.Add(new object[] { 4, DateTime.Parse("2021-04-09") });
masterTable.Rows.Add(new object[] { 5, DateTime.Parse("2020-06-24") });
DataTable detailTable = new DataTable("Detail");
detailTable.Columns.Add("Id", typeof(int));
detailTable.Columns.Add("IMasterId", typeof(int));
detailTable.Columns.Add("Name", typeof(string));
detailTable.Columns.Add("Quantity", typeof(int));
detailTable.Rows.Add(new object[] { 1, 1, "item1", 25 });
detailTable.Rows.Add(new object[] { 2, 1, "item2", 30 });
detailTable.Rows.Add(new object[] { 3, 1, "item3", 20 });
detailTable.Rows.Add(new object[] { 4, 2, "item1", 25 });
detailTable.Rows.Add(new object[] { 5, 2, "item2", 20 });
detailTable.Rows.Add(new object[] { 6, 3, "item1", 35 });
detailTable.Rows.Add(new object[] { 7, 4, "item4", 25 });
detailTable.Rows.Add(new object[] { 8, 5, "item1", 25 });
detailTable.Rows.Add(new object[] { 9, 5, "item3", 29 });
string[] items = detailTable.AsEnumerable().Select(x => x.Field<string>("Name")).OrderBy(x => x).Distinct().ToArray();
DataTable pivot = new DataTable("Pivot");
pivot.Columns.Add("Year", typeof(int));
foreach (string item in items)
{
pivot.Columns.Add(item, typeof(int));
}
var joinTable = (from m in masterTable.AsEnumerable().OrderBy(x => x.Field<DateTime>("Date"))
join d in detailTable.AsEnumerable() on m.Field<int>("Id") equals d.Field<int>("Id")
select new {id = m.Field<int>("Id"), year = m.Field<DateTime>("Date").Year, d = d}
).GroupBy(x => x.year).ToList();
foreach (var date in joinTable)
{
DataRow row = pivot.Rows.Add();
row["Year"] = date.Key;
var names = date.GroupBy(x => x.d.Field<string>("Name")).Select(x => new {name = x.Key, quant = x.Sum(y => y.d.Field<int>("Quantity"))});
foreach (var name in names)
{
row[name.name] = name.quant;
}
}
}
}
}

How to select records with MAX Id that group by multiple columns in LINQ to SQL

I need to select the last record of particular columns. I have the following records
WarehouseId | ItemId | SubItemId | DeliveryGroupId | Other Columns
1 | 1 | 1 | 1 | ...
1 | 1 | 1 | 2 | ...
1 | 1 | 1 | 3 | ...
1 | 1 | 2 | 1 | ...
1 | 1 | 2 | 2 | ...
1 | 2 | 1 | 1 | ...
Then I only want to select the MAX(DeliveryGroupId) for each WarehouseId | ItemId | SubItemId. The result should be:
WarehouseId | ItemId | SubItemId | DeliveryGroupId | Other Columns
1 | 1 | 1 | 3 | ...
1 | 1 | 2 | 2 | ...
1 | 2 | 1 | 1 | ...
In SQL, it is very simple to do:
SELECT *
FROM [dbo].[tblOrderDeliveryGroup] t1
WHERE [DeliveryGroupId] IN
(
SELECT MAX([DeliveryGroupId])
FROM [dbo].[tblOrderDeliveryGroup] t2
WHERE (t1.[WarehouseId] = t2.[WarehouseId]) AND (t1.[ItemId] = t2.[ItemId]) AND (t1.[SubItemId] = t2.[SubItemId])
GROUP BY [WarehouseId], [ItemId], [SubItemId]
);
The question is, how do I translate that SQL statement into LINQ-to-SQL?
Thanks
UPDATE
So far, this is my solution. It is very ugly and surely not efficient.
var vLastRecs = (from rec in tblOrderDeliveryGroups.AsNoTracking()
group rec by new { rec.WarehouseId, rec.ItemId, rec.SubItemId } into grec
select new
{
grec.Key.WarehouseId,
grec.Key.ItemId,
grec.Key.SubItemId,
DeliveryGroupId = grec.Max(rec => rec.DeliveryGroupId)
});
return (from rec in tblOrderDeliveryGroups.AsNoTracking()
where vLastRecs.Any(lrec => (rec.WarehouseId == lrec.WarehouseId) && (rec.ItemId == lrec.ItemId) && (rec.SubItemId == lrec.SubItemId) && (rec.DeliveryGroupId == lrec.DeliveryGroupId))
select rec).ToList();
Is it possible to improve it?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("WarehouseId", typeof(int));
dt.Columns.Add("ItemId", typeof(int));
dt.Columns.Add("SubItemId", typeof(int));
dt.Columns.Add("DeliveryGroupId", typeof(int));
dt.Rows.Add(new object[] {1,1,1,1});
dt.Rows.Add(new object[] {1,1,1,2});
dt.Rows.Add(new object[] {1,1,1,3});
dt.Rows.Add(new object[] {1,1,2,1});
dt.Rows.Add(new object[] {1,1,2,2});
dt.Rows.Add(new object[] {1,2,1,1});
DataTable dt2 = dt.AsEnumerable()
.OrderByDescending(x => x.Field<int>("DeliveryGroupId"))
.GroupBy(x => new { warehouse = x.Field<int>("WarehouseId"), item = x.Field<int>("ItemId"), subitem = x.Field<int>("SubItemId")})
.Select(x => x.FirstOrDefault())
.CopyToDataTable();
}
}
}
Here is a solution using classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
OrderDeliveryGroups tblOrderDeliverGroups = new OrderDeliveryGroups();
List<AsNoTracking> vLastRecs = tblOrderDeliverGroups.AsNoTracking()
.OrderByDescending(x => x.DeliverGroupId)
.GroupBy(x => new { x.WarehouseId, x.ItemId, x.SubItemId})
.Select(x => x.FirstOrDefault())
.ToList();
}
}
public class OrderDeliveryGroups
{
public List<AsNoTracking> AsNoTracking()
{
return new List<AsNoTracking>() {
new AsNoTracking() { WarehouseId = 1, ItemId = 1, SubItemId = 1, DeliverGroupId = 1 },
new AsNoTracking() { WarehouseId = 1, ItemId = 1, SubItemId = 1, DeliverGroupId = 2 },
new AsNoTracking() { WarehouseId = 1, ItemId = 1, SubItemId = 1, DeliverGroupId = 3 },
new AsNoTracking() { WarehouseId = 1, ItemId = 1, SubItemId = 2, DeliverGroupId = 1 },
new AsNoTracking() { WarehouseId = 1, ItemId = 1, SubItemId = 2, DeliverGroupId = 2 },
new AsNoTracking() { WarehouseId = 1, ItemId = 2, SubItemId = 1, DeliverGroupId = 1 }
};
}
}
public class AsNoTracking
{
public int WarehouseId { get; set; }
public int ItemId { get; set; }
public int SubItemId { get; set; }
public int DeliverGroupId { get; set; }
}
}

how to DataTable split into mutiple datatables c#

Gender Age Category
--------------------------------
Male | 10 | 2
Female | 15 | 1
Trans | 13 | 3
Female | 10 | 1
Male | 20 | 2
i have a datatable with above values. Male CategoryId is 2. in above table there are total 2 Males rows. based on Category, merged two rows and divide into a seperate datatable.
My required output is :-
Datatable 1
Gender Age Category
--------------------------------
Male | 10 | 2
Male | 20 | 2
DataTable 2
Gender Age Category
--------------------------------
Female | 15 | 1
Female | 10 | 1
DataTable 3
Gender Age Category
--------------------------------
Trans | 13 | 3
var view = sourceDataTable.DefaultView;
view.RowFilter = "Category = 2";
var maleDataTable = view.ToTable();
view.RowFilter = "Category = 1";
var femaleDataTable = view.ToTable();
view.RowFilter = "Category = 3";
var transDataTable = view.ToTable();
Here you go:
List<DataTable> result = DTHead.AsEnumerable()
.GroupBy(row => row.Field<DataType>("Category"))
.Select(g => g.CopyToDataTable())
.ToList();
For more details please check this post:Split Tables
See following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication48
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Gender", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Category", typeof(int));
dt.Rows.Add(new object[] {"Male", 10, 2});
dt.Rows.Add(new object[] {"Female", 15, 1});
dt.Rows.Add(new object[] {"Trans", 13, 3});
dt.Rows.Add(new object[] {"Female", 10, 1});
dt.Rows.Add(new object[] {"Male", 20, 2});
DataTable dt1 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Male").CopyToDataTable();
DataTable dt2 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Feale").CopyToDataTable();
DataTable dt3 = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == "Trans").CopyToDataTable();
}
}
}
Here is a more generic solution that get every type in a column :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication48
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Gender", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Category", typeof(int));
dt.Rows.Add(new object[] { "Male", 10, 2 });
dt.Rows.Add(new object[] { "Female", 15, 1 });
dt.Rows.Add(new object[] { "Trans", 13, 3 });
dt.Rows.Add(new object[] { "Female", 10, 1 });
dt.Rows.Add(new object[] { "Male", 20, 2 });
//updated code
string[] rowNames = dt.AsEnumerable().Select(x => x.Field<string>("Gender")).Distinct().ToArray();
DataSet ds = new DataSet();
foreach (string gender in rowNames)
{
DataTable newDt = dt.AsEnumerable().Where(x => x.Field<string>("Gender") == gender).CopyToDataTable();
newDt.TableName = gender;
ds.Tables.Add(newDt);
}
}
}
}

How to use group by in rows?

How to group the below data ? as I am looping through the collection and it gives me only 1 row as there is no grouping in place.
I have to group the below records based on Id column and if there are repeating Ids ? I have to populate model with that many rows.
id name trID trName
1 a 5 x
2 b 6 y
2 c 7 z
3 d 8 m
3 e 9 n
4 f 10 0
class DataModel
{
Public int Id{get;set;}
Public string name{get;set;}
Public RepeatedIDs RepeatedIDCollection{get;set;}
}
class RepeatedIDs
{
Public int trId{get;set;}
Public string trname{get;set;}
}
(from DataRow dr in dataTable.Rows
select new IdModel
{
Id = Convert.ToInt32(dr["ID"]),
name = Convert.ToString(dr["name"]),
// need to group the records here and populate below mode with that many rows
RepeatedIDCollection = new List<RepeatedIDs>
{
new RepeatedIDs()
{
trId = Convert.ToInt32(dr["trId"]),
trname = Convert.ToString(dr["trname"]),
}
}
}).ToList();
What you need is:
var query = dataTable.AsEnumerable()
.GroupBy(r => r.Field<int>("ID"))
.Select(grp => new DataModel
{
Id = grp.Key,
name = String.Join(",", grp.Select(t => t.Field<string>("name"))), //Because there could be multiple names
RepeatedIDCollection = grp.Select(t => new RepeatedIDs
{
trId = t.Field<int>("trID"),
trname = t.Field<string>("trName")
}).ToList(),
});
What this query is doing:
Grouping the data based on ID column in DataTable
Later selecting an object of DataModel
The Id in DataModel is the key from group.
There will be multiple names in the grouped data
Later it creates a List<RepeatedIDCollection> by getting the trId and trname from grouped collection.
Make sure you specify the correct types in Field method.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof (int));
dt.Columns.Add("name", typeof (string));
dt.Columns.Add("trID", typeof (int));
dt.Columns.Add("trName", typeof (string));
dt.Rows.Add(new object[] { 1,"a", 5,"x"});
dt.Rows.Add(new object[] { 2,"b", 6,"y"});
dt.Rows.Add(new object[] { 2,"c", 7,"z"});
dt.Rows.Add(new object[] { 3,"d", 8,"m"});
dt.Rows.Add(new object[] { 3,"e", 9,"n"});
dt.Rows.Add(new object[] { 4,"f", 510,"0"});
var groups = dt.AsEnumerable().GroupBy(x => x.Field<int>("id")).ToList();
}
}
}

Doing pivot with LINQ

I've got this problem..I have a CSV file in the following format (customer, bought item pair):
customer1 item1
customer1 item2
customer1 item3
customer2 item4
customer2 item2
customer3 item5
customer3 item1
customer3 item2
customer4 item1
customer4 item2
customer5 item5
customer5 item1
Now, I wish to show in query results:
item x; item y; how many customers have bought itemx and item together
For example:
item1 item2 3 (because cust1 and cust2 and cust3 bought item1 and item2 together)
item1 item5 1 (because cust5 and cust3 bought item1 and item5 together)
The query return all possible combinations of items that customers have bought in pairs. Also notice that Pair(x, y) is the same as Pair(y, x).
An SQL query would look like this:
SELECT a1.item_id, a2.item_id, COUNT(a1.cust_id) AS how_many_custs_bought_both
FROM data AS a1
INNER JOIN data AS a2
ON a2.cust_id=a1.cust_id AND a2.item_id<>a1.item_id AND a1.item_id<a2.item_id
GROUP BY a1.item_id, a2.item_id
How would you do that in C# 1) using regular for/foreach loops 2) using LINQ ?
I tried doing it in LINQ first but stuck when I noticed that LINQ doesn't support multiple equals keyword in join clause. Then I tried doing using normal loops, however, it became so unefficient that it could only process like 30 lines (of CSV file rows) per second.
Please advise!
Using LINQ (and following the first 5 lines from Tim's answer) combining the chained method syntax with the query syntax for the join part:
var custItems = new [] {
new { customer = 1, item = 1 },
new { customer = 1, item = 2 },
new { customer = 1, item = 3 },
new { customer = 2, item = 4 },
new { customer = 2, item = 2 },
new { customer = 3, item = 5 },
new { customer = 3, item = 1 },
new { customer = 3, item = 2 },
new { customer = 4, item = 1 },
new { customer = 4, item = 2 },
new { customer = 5, item = 5 },
new { customer = 5, item = 1 }
};
};
var pairs = custItems.GroupBy(x => x.customer)
.Where(g => g.Count() > 1)
.Select(x => (from a in x.Select( y => y.item )
from b in x.Select( y => y.item )
where a < b //If you want to avoid duplicate (a,b)+(b,a)
// or just: where a != b, if you want to keep the dupes.
select new { a, b}))
.SelectMany(x => x)
.GroupBy(x => x)
.Select(g => new { Pair = g.Key, Count = g.Count() })
.ToList();
pairs.ForEach(x => Console.WriteLine(x));
EDIT: Forgot that OP wanted pair ocurrence count, added another .GroupBy() magic.
EDIT: Completed the example to show what it would output:
{ Pair = { a = 1, b = 2 }, Count = 3 }
{ Pair = { a = 1, b = 3 }, Count = 1 }
{ Pair = { a = 2, b = 3 }, Count = 1 }
{ Pair = { a = 2, b = 4 }, Count = 1 }
{ Pair = { a = 1, b = 5 }, Count = 2 }
{ Pair = { a = 2, b = 5 }, Count = 1 }
EDIT: rolled back and changed strings to integers, as OP shows a dataset with integers as IDs, and that removes the need for .GetHashCode()
Perhaps:
var lines = File.ReadLines(csvFilePath);
var custItems = lines
.Select(l => new { split = l.Split() })
.Select(x => new { customer = x.split[0].Trim(), item = x.split[1].Trim() })
.ToList();
var groups = from ci1 in custItems
join ci2 in custItems
on ci1.customer equals ci2.customer
where ci1.item != ci2.item
group new { Item1 = ci1.item, Item2 = ci2.item } by new { Item1 = ci1.item, Item2 = ci2.item } into ItemGroup
select ItemGroup;
var result = groups.Select(g => new
{
g.Key.Item1,
g.Key.Item2,
how_many_custs_bought_both = g.Count()
});
Note that the materialization with ToList is important when the file is large because of the self-join.
{ Item1 = item1, Item2 = item2, how_many_custs_bought_both = 3 }
{ Item1 = item1, Item2 = item3, how_many_custs_bought_both = 1 }
{ Item1 = item2, Item2 = item1, how_many_custs_bought_both = 3 }
{ Item1 = item2, Item2 = item3, how_many_custs_bought_both = 1 }
{ Item1 = item3, Item2 = item1, how_many_custs_bought_both = 1 }
{ Item1 = item3, Item2 = item2, how_many_custs_bought_both = 1 }
{ Item1 = item4, Item2 = item2, how_many_custs_bought_both = 1 }
{ Item1 = item2, Item2 = item4, how_many_custs_bought_both = 1 }
{ Item1 = item5, Item2 = item1, how_many_custs_bought_both = 2 }
{ Item1 = item5, Item2 = item2, how_many_custs_bought_both = 1 }
{ Item1 = item1, Item2 = item5, how_many_custs_bought_both = 2 }
{ Item1 = item2, Item2 = item5, how_many_custs_bought_both = 1 }
You can write some like this:
IDictionary<int, int> pivotResult = customerItems.ToLookup(c => c.Customer)
.ToDictionary(x=>x.Key, y=>y.Count());
Working LINQ example, not too pretty!
using System;
using System.Collections.Generic;
using System.Linq;
class Data
{
public Data(int cust, int item)
{
item_id = item;
cust_id = cust;
}
public int item_id { get; set; }
public int cust_id { get; set; }
static void Main(string[] args)
{
var data = new List<Data>
{new Data(1,1),new Data(1,2),new Data(1,3),
new Data(2,4),new Data(2,2),new Data(3,5),
new Data(3,1),new Data(3,2),new Data(4,1),
new Data(4,2),new Data(5,5),new Data(5,1)};
(from a1 in data
from a2 in data
where a2.cust_id == a1.cust_id && a2.item_id != a1.item_id && a1.item_id < a2.item_id
group new {a1, a2} by new {item1 = a1.item_id, item2 = a2.item_id}
into g
select new {g.Key.item1, g.Key.item2, count = g.Count()})
.ToList()
.ForEach(x=>Console.WriteLine("{0} {1} {2}",x.item1,x.item2,x.count))
;
Console.Read();
}
}
Output:
1 2 3
1 3 1
2 3 1
2 4 1
1 5 2
2 5 1

Categories