0

I am converting an MVC C# application to VB.

I'm running across problem converting Linq.
I've got to do a number of these, so prefer a nice explanation.
When I convert to VB I seem to get null int error which I'm not getting in the C# version.

System.NotSupportedException: 'Unable to cast the type 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.'

I've tried online converter but they are not giving valid answers.

C#

public List<notesselect> getNotes(int? catid, int? userid, int page = 1, int size = 10)
{
    // pagination stuff
    int startindex = 1;
    startindex = ((page - 1) * size);
    //get all noted which is created by user or shared to user
    var notes = (from c in db.Notes
                 join Category in db.Categorys on c.CategoryId equals Category.CategoryId
                 join d in db.Users on c.CreatedBy equals d.UserId
                 join p in db.NotesAuth on new { NotesId = c.NotesId, isactive = true } equals new { NotesId = p.NotesId, isactive = p.IsActive } into ps
                 from p in ps.DefaultIfEmpty()
                 where c.IsActive == true
                 && (c.CreatedBy == userid || p.UserId == userid)
                 && (catid != null ? c.CategoryId == catid : true)
                 select new notesselect
                 {
                     CategoryId = c.CategoryId,
                     CategoryName = Category.Category,
                     title = c.Title,
                     createby = d.FirstName + " " + d.LastName,
                     noteid = c.NotesId,
                     createdate = c.ChangeDate,
                     lastModifiedOn = c.ChangeDate.ToString()
                 }).OrderByDescending(x => x.createdate);
    if (page > 0)
    {
        return notes.Skip(startindex).Take(size).ToList();
    }
    else
    {
        return notes.ToList();
    }
}

/// <summary>
/// Get Total Count of Notes 
/// </summary>
/// <param name="catid">Category Id</param>
/// <param name="userid">User Id</param>
/// <returns></returns>
public int getNoteCount(int? catid, int? userid)
{
    var total = (from c in db.Notes
                 join Category in db.Categorys on c.CategoryId equals Category.CategoryId
                 join d in db.Users on c.CreatedBy equals d.UserId
                 join p in db.NotesAuth on new { NotesId = c.NotesId, isactive = true } equals new { NotesId = p.NotesId, isactive = p.IsActive } into ps
                 from p in ps.DefaultIfEmpty()
                 where c.IsActive == true
                 && (c.CreatedBy == userid || p.UserId == userid)
                  && (catid != null ? c.CategoryId == catid : true)
                 select c).Count();
    return total;
}

Vb Converted code

Public Function getNotes(ByVal catid? As Integer, ByVal userid? As Integer, Optional ByVal page As Integer = 1, Optional ByVal size As Integer = 10) As List(Of notesselect)
    ' pagination stuff
    Dim startindex As Integer = 1
    startindex = 1 '((page - 1) * size)
    'get all noted which is created by user or shared to user
    Dim notes = (From c In db.Notes
                 Join Category In db.Categorys On CType(c.CategoryId, Integer?) Equals CType(Category.CategoryId, Integer?)
                 Join d In db.Users On c.CreatedBy Equals CType(d.UserId, Integer?)
                 Group Join p In db.NotesAuth On New With {
          .NotesId = c.NotesId,
          .isactive = True
        } Equals New With {
          .NotesId = p.NotesId,
         .isactive = p.IsActive
        } Into ps = Group
                 From p In ps.DefaultIfEmpty()
                 Where c.IsActive = True AndAlso (userid.Equals(c.CreatedBy) OrElse userid.Equals(p.UserId)) AndAlso (If(catid IsNot Nothing, catid.Equals(c.CategoryId), True))
                 Select New notesselect With {
            .CategoryId = c.CategoryId,
            .CategoryName = Category.Category,
            .title = c.Title,
            .createby = d.FirstName & " " & d.LastName,
            .noteid = c.NotesId,
            .createdate = c.ChangeDate,
            .lastModifiedOn = CStr(c.ChangeDate.ToString)
        }).OrderByDescending(Function(x) x.createdate)
    If page > 0 Then
        Return notes.Skip(startindex).Take(size).ToList()
    Else
        Return notes.ToList()
    End If
    'Dim InvoiceNo = If(dbContext.table.where(Function(x) DirectCast(x.ICOD, System.Nullable(Of Integer))), "NCOD")
End Function

''' <summary>
''' Get Total Count of Notes 
''' </summary>
''' <param name="catid">Category Id</param>
''' <param name="userid">User Id</param>
''' <returns></returns>
Public Function getNoteCount(ByVal catid? As Integer, ByVal userid? As Integer) As Integer
    Dim total = (
        From c In db.Notes
        Join Category In db.Categorys On CType(c.CategoryId, Integer?) Equals Category.CategoryId
        Join d In db.Users On CType(c.CreatedBy, Integer?) Equals CType(d.UserId, Integer?)
        Group Join p In db.NotesAuth On New With {
            Key .NotesId = c.NotesId,
            Key .isactive = True
        } Equals New With {
            Key .NotesId = p.NotesId,
            Key .isactive = p.IsActive
        } Into ps = Group
        From p In ps.DefaultIfEmpty()
        Where c.IsActive = True AndAlso (userid.Equals(c.CreatedBy) OrElse userid.Equals(p.UserId)) AndAlso (If(catid IsNot Nothing, catid.Equals(c.CategoryId), True))
        Select c).Count()
    Return total
End Function
3
  • Why did you add those CType(, Integer?)? C# is stricter than VB, if it works there without them, it will work here. Commented Jun 10, 2020 at 7:41
  • 1
    I don't know where i'm getting the null error. Its was an after thought, while trying to find a solution. Belt and braces Commented Jun 10, 2020 at 9:05
  • But your belts and suspenders seem backwards - somewhere your code expects an Object and is getting a Int32?. Can you comment out parts of the code to see where? LINQPad is a good tool for playing around with queries like this. (Note: Also, you can use C# code in a VB.Net project from a separate DLL pretty easily.) Can you provide the class definitions, especially notesselect? Commented Jun 11, 2020 at 0:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.