i have the following vb.net LINQ Query
Dim q = From row In dx.AsEnumerable
Group row By G = New With {.Cat = row.Field(Of Integer)("catalogid")}.Cat,
New With {.Backorder = row.Field(Of Integer)("backorder")}.Backorder Into pg = Group
Select New With
{
.Cat = pg.Cat,
.Backorder = pg.Sum(Function(x) x.Backorder)
}
i have this datatable called dx
catalogid backorder
1 5
1 10
2 1
2 5
i want to Sum backorder column where catalogid is the same so the result is the following
catalogid backorder
1 15
2 6
in the Select new with part, what is wrong?
Try following...
var result = dx.AsEnumerable()
.GroupBy(row => row.Field<int>("catalogid"))
.Select(group => group.Sum(item => item.Field<int> ("backorder")).CopyToDataTable();
or
var result= from s in dx.AsEnumerable()
group s by s.Field<int>("catalogid") into grp
select new {
CatalogId= grp.Key,
Back Order = grp.Sum(r => r.Field<int>("backorder"))
};
DataTable datatbl = result.CopyToDataTable();
Related
my table data is like this,
col1 col2 col3
A A1 1
B B1 1
A A2 1
B B2 1
C C1 1
D D1 1
E E1 1
A A3 1
so I want data like this
A 3 3
B 2 2
C 1 1
D 1 1
E 1 1
how can i query in linq please
var result = db.MyData
.GroupBy(x => x.Column1)
.Select(x => new {
Column1 = x.Key,
Count = x.Count(),
Sum = x.Sum(y => y.Column3)
});
Here is a sample:
void Main()
{
var myData = new[] {
new {col1="A", col2="A1", col3=1},
new {col1="B", col2="B1", col3=1},
new {col1="A", col2="A2", col3=1},
new {col1="B", col2="B2", col3=1},
new {col1="C", col2="C1", col3=1},
new {col1="D", col2="D1", col3=1},
new {col1="E", col2="E1", col3=1},
new {col1="A", col2="A3", col3=1}
};
var result = myData
.GroupBy(x => x.col1)
.Select(x => new
{
Column1 = x.Key,
Count = x.Count(),
Sum = x.Sum(y => y.col3)
});
result.Dump(); // LinqPad
}
Output:
Column1,Count,Sum
A 3 3
B 2 2
C 1 1
D 1 1
E 1 1
I have a datatable and I want to use LINQ to get the equivalent of
Select barid, count(distinct bar) as barCount
From myTable
Group By barid
The data is in the form:
barid bar
4 1
4 1
4 1
12 2
12 2
12 2
12 3
13 1
13 2
13 3
The result should be:
barid, barCount
4 1
12 2
13 3
Can you help?
I cannot translate it into vb.net but the c# equivalent would be:
var result = from record in data
group record by record.barid into grp
select new
{
barid = grp.Key,
barCount = grp.Select( item => item.bar ).Distinct( ).Count( )
};
Considering your DataTable columns to be of type Integer, you can use below query:-
Dim result = dt.AsEnumerable().GroupBy(Function(x) x.Field(Of Integer)("barid")) _
.[Select](Function(x) New With { _
Key .barid = x.Key, _
Key .barCount = x.[Select](Function(z) z.Field(Of Integer)("bar")) _
.Distinct().Count()})
Then you can simply use the ForEach like this:-
For Each x In result
Console.WriteLine("barid {0}",x.barid)
Console.WriteLine("barCount {0}", x.barCount)
Console.WriteLine("---------------")
Next
This is giving me the following output:-
How do I perform group in LINQ inside vb code (dot.net v4.0) with DataTable and sum on the group?
In the sample below I need to add group by GroupName, ProductName and perform sum on QTY. The columns, order and where should remain as in sample, I just need to add the group and sum. The format should remain the same (getting row using e("FieldName")).
Dim ordersTable As DataTable = _dsProd.Tables("tblProductSummary")
Dim query =
(From e In ordersTable
Where (e("Type").ToString() = "1" Or IsDBNull(e("Type")))
Order By e("GroupSortOrder") Ascending, e("ProductName")
Select
GroupName = e("GroupName"),
ProductName = e("ProductName"),
QTY = e("QTY"),
Type= e("Type")
)
Dim query =
(From e In ordersTable
Where (e("Type").ToString() = "1" Or IsDBNull(e("Type")))
Order By e("GroupSortOrder") Ascending, e("ProductName")
Group e By Key = New With {
.ProductName = e("ProductName"),
.GroupName = e("GroupName")
} Into Group
Select New With {
.ProductName = Key.ProductName,
.GroupName = Key.GroupName,
.Sum = Group.Sum(Function(x) x("QTY"))
})
I have,
A list, MyList, of objects with fields:
string A;
string B;
Conceptually, this is similar to a two column SQL Table with columns A, B.
I'm trying to create a linq expression that would produce the three column result set of this T-SQL on such a conceptual table:
SELECT A, B, COUNT(B)
FROM T1
GROUP BY A, B
That is, if I had a table such as:
A B
----------
x g
x g
x g
x s
y g
y g
I would expect:
A B COUNT(B)
-------------------------
x g 3
x s 1
y g 2
My best efforts were this:
var result = from MyObjs in MyList
group MyObjs by new { MyObjs.A, MyObjs.B } into g
select new { g.Key.A, g.Key.B, g.Key.B.Count() }
But the count appears to return the total number of B's not the number of B's per multiple column group. How can this be fixed?
Try this.... (off the top of my head)
var result = from MyObjs in MyList
group MyObjs by new { MyObjs.A, MyObjs.B } into g
select new { g.Key.A, g.Key.B, MyCount = g.Count() }
Or if you prefer...
var result = MyList.GroupBy(x => new {x.A, x.B})
.Select(g => new {g.Key.A, g.Key.B, MyCount = g.Count()});
How do I return a top 5 with rank number using linq?
Dim Top5 = From A In DAO.Cache.Select(Of VO.Empresa).Take(5) Select A.Nome
I would like this result:
Rank Name
1 "Example Name"
2 "Example Name"
3 "Example Name"
4 "Example Name"
5 "Example Name"
You need to use the Select overload which provides the index:
Dim Top5 = DAO.Cache.Take(5).Select(Function(A, Index) New With { .Rank = Index, .Name = A.Nome })
(I kept the property spelling .Nome - though I suspect it may need to be .Name)
If grdDetail.RowCount < 10 Then
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = "00" & grdDetail.RowCount
ElseIf grdDetail.RowCount > 10 And grdDetail.RowCount < 100 Then
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = "0" & grdDetail.RowCount
Else
grdDetail.CurrentRow.Cells(OrderNo.Name).Value = grdDetail.RowCount
End If
I'm not entirely sure I understand your question entirely: but I'm assuming you want to order your list to produce the top 5 in Rank - ascending order?
You can quite easily do this with the built in LINQ ordering syntax:
VB:
Dim Top5 = From o in objects Order By o.Rank Ascending Select o
C#: var top5 = from o in objects orderby o.Rank ascending select o
(surprising similar in this case /giggle)
For example, you could do the following:
C#:
void Main()
{
List<MyObject> objs = new List<MyObject>();
objs.Add(new MyObject{ Rank = 1, Message = "NUMBER ONE"});
objs.Add(new MyObject{ Rank = 3, Message = "NUMBER THREE"});
objs.Add(new MyObject{ Rank = 5, Message = "NUMBER FIVE"});
objs.Add(new MyObject{ Rank = 4, Message = "NUMBER FOUR"});
objs.Add(new MyObject{ Rank = 2, Message = "NUMBER TWO"});
var sortedobjs = from o in objs
orderby o.Rank ascending
select o;
Console.WriteLine(sortedobjs.ToList());
}
public class MyObject
{
public int Rank {get; set;}
public string Message {get; set;}
}
Which would spit out:
Rank Message
1 NUMBER ONE
2 NUMBER TWO
3 NUMBER THREE
4 NUMBER FOUR
5 NUMBER FIVE
HTH.