LINQ - No data to copy to datatable error - vb.net

I encountered an error whenever there's no data to copy to datatable. I'm not able to assign the copied datatable to my variable.
My code ResultDT as Datatable format:
ResultDT = (From d In MainDT.AsEnumerable
Group d By k=d("UPDATED_BRN").ToString.Trim Into grp=Group
Let check = grp.Any(Function ( r ) CheckOrderDT.AsEnumerable.Any(Function (x) x("ACCNT_ID").ToString.Trim.Equals(r("ROW_ID").ToString.Trim )))
From g In grp
Let ra = New Object(){g(0),g(1),g(2),g(3),g(4),g(5),g(6),g(7),g(8),g(9), If(check, "Available",g(10))}
Select ResultDT.Rows.Add(ra)).CopyToDataTable
So I try to change it to check first before copy to datatable but I got a different error.
I change it to - FilteredRows as System.Collections.Generic.IEnumerable<System.Data.DataRow>:
FilteredRows = (From d In MainDT.AsEnumerable
Group d By k=d("UPDATED_BRN").ToString.Trim Into grp=Group
Let check = grp.Any(Function ( r ) CheckOrderDT.AsEnumerable.Any(Function (x) x("ACCNT_ID").ToString.Trim.Equals(r("ROW_ID").ToString.Trim )))
From g In grp
Let ra = New Object(){g(0),g(1),g(2),g(3),g(4),g(5),g(6),g(7),g(8),g(9), If(check, "Available",g(10))}
Select ResultDT.Rows.Add(ra))
ResultDT = If(FilteredRows.Any, FilteredRows.CopyToDatatable, MainDT.Clone)
But still encountered an error - Cannot assign to ResultDT.
Can anyone suggest a better idea to cater if there's not data in the datatable?

Related

Unable to cast a object of type 'WhereSelectEnumerableIterator`2 [VB $ AnonymousType_8`9

I have written a query by using the linq to Datatable, but I don't know what is the type to use to declare my variable queryArchi? I have tried the type System.Data.EnumerableRowCollection(Of Object) but I get a message error :
Unable to cast a object of type 'WhereSelectEnumerableIterator2 [VB $ AnonymousType_89
Code:
Dim queryArchi As System.Data.EnumerableRowCollection(Of Object)
Dim queryStructure
If tf = 0 Then
queryArchi = (From b In DataBien.AsEnumerable()
Group b By b!code_projet, b!code_operation, b!code_type, b!Designation_part, b!code_part Into Group
Select code_projet, code_operation, code_type, NBP = Group.Count())
Else
queryArchi = (From b In DataBien.AsEnumerable()
Group b By b!code_projet, b!code_operation, b!code_type, b!Designation_part, b!code_part, b!Titré Into Group
Select code_projet, code_operation, code_type, Titré, NBP = Group.Count())
End If
If vil <> "" Then queryArchi = queryArchi.Where(Function(d) d.code_projet = vil)
queryStructure = queryArchi.ToList

linq to sql The Grid view

I make sales and procurement system and when I work a query, or search the invoice number, the results appear in palm leaves View, but appears only in a row while the bill by no more than one row
And this was used by code
i.m used vb.net and database sql and b yway linq to sql
Try
Dim data = (From d In DBVariable.Data.masterfatoras
From f In DBVariable.Data.fatoras
From na In DBVariable.Data.asnafs
From sup In DBVariable.Data.suppliers
Where d.ID = f.mid Where d.num.Contains(txt)
Select d, f, na, sup).FirstOrDefault
TextBoxX1.Text = data.d.num
nammord.Text = (From supp In DBVariable.Data.suppliers Where data.d.idmord = supp.ID Select supp.Name).Single()
txtmord.Text = (From supp In DBVariable.Data.suppliers Where data.d.idmord = supp.ID Select supp.Code).Single()
adrmord.Text = (From supp In DBVariable.Data.suppliers Where data.d.idmord = supp.ID Select supp.Address).Single()
nodaf.Text = data.d.nodfa
' ''نفاصيل الفاتورة
For p As Integer = 0 To gridshraa.Rows.Count - 1
gridshraa.Rows(p).Cells(2).Value = (From asna In DBVariable.Data.asnafs Where data.f.idname = asna.ID Select asna.Name).Single()
gridshraa.Rows(p).Cells(1).Value = (From asna In DBVariable.Data.asnafs Where data.f.idname = asna.ID Select asna.code).Single()
gridshraa.Rows(p).Cells(3).Value = (From asna In DBVariable.Data.asnafs Where data.f.idname = asna.ID Select asna.unit).Single()
gridshraa.Rows(p).Cells(4).Value = data.f.qty
gridshraa.Rows(p).Cells(5).Value = data.f.price
gridshraa.Rows(p).Cells(6).Value = data.f.totprice
Next
Catch
End Try

Linq IEnumerable to entity cast error

Please refer to my following code. An exception error occurs at the return value.
Unable to cast object of type 'System.Data.Objects.ObjectQuery1[VB$AnonymousType_23[System.String,System.String,System.String]]' to type 'System.Collections.Generic.List`1[MyEntities.stk_cust]'.
Public Function GetCustomerProdListToGrid() As List(Of stk_cust)
Dim result = From s In Db.stk_cust
Group Join d In Db.s_armaster On d.dbcode Equals s.dbcode Into stk_ar = Group
From d In stk_ar.DefaultIfEmpty()
Order By s.stk_code, s.dbcode Ascending
Select s.stk_code, s.dbcode, d.name
Return result
End Function
I've tried the following method at select section but error turns out the same
Select New With {.stk_code = s.stk_code,
.dbcode = s.dbcode,
.name = d.name
}
Any help is highly appreciated.
If you want to return a List(Of stk_cust) you need to create instances of stk_cust instead of using an anonymous type. Presuming that those are the properties:
....
Select New stk_cust() With {
.stk_code = s.stk_code,
.dbcode = s.dbcode,
.name = d.name
}
Return result.ToList()
Maybe you could also select the object directly without needing to create a new one:
Dim result = From s In Db.stk_cust
Group Join d In Db.s_armaster On d.dbcode Equals s.dbcode Into stk_ar = Group
From d In stk_ar.DefaultIfEmpty()
Order By s.stk_code, s.dbcode Ascending
Select s
Return result.ToList()

how to perform groupby in linq on DataTable inside vb code?

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"))
})

Linq To SQL: left join and vb.NET

I have this SQL query:
SELECT TOP 1 r.* FROM dbo.Request r
LEFT JOIN dbo.Process p
ON r.IdReq = p.IdReq
WHERE IdProcess Is NULL
ORDER BY r.ReqDate ASC
I want write it in a vb.net procedure, using Linq To SQL syntax:
Private Function getOldestRequest() As Request
Dim dc As ProcessDataContext = new ProcessDataContext(ConfigurationManager.ConnectionStrings("...").ConnectionString);
Dim pr = From r In dc.Request
From p In dc.Process.Where(Function(v) v.IdReq = r.IdReq And v.idProcess Is Nothing)
Select New {Request = r}
Return pr
End Function
This is what I wrote, but I get error, on the Return line.
LINQ statements return IEnumerable objects, and your method is returning a single Request object. In your case looks like you only need to return the first result of the LINQ statement.
Something like:
Dim pr = (From r In dc.Request
From p In dc.Process.Where(Function(v) v.IdReq = r.IdReq And v.idProcess Is Nothing)
Select New {Request = r}).FirstOfDefault
Return pr
Change your Select to
Select r
And your return to
Return pr.OrderBy(Function(v) v.RecDate).FirstOrDefault