CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Count in VB.Net may need to be converted to Count() in C#

Open CardenInsurance opened this issue 2 years ago • 2 comments

VB.Net input code

Dim lstCropRecordsToDelete As List(Of PolicyCrop) = <snip>
If lstCropRecordsToDelete.Count > 0 Then
    ' do stuff
End If

Erroneous output

var lstCropRecordsToDelete = <snip>
if (lstCropRecordsToDelete.Count > 0)
{
    // do stuff
}

This gets Visual Studio error: CS0019 Operator '>' cannot be applied to operands of type 'method group' and 'int'

Expected output (add parentheses)

var lstCropRecordsToDelete = <snip>
if (lstCropRecordsToDelete.Count() > 0)
{
    // do stuff
}

Details

  • Product in use: VS extension
  • Version in use: 9.0.2.0

CardenInsurance avatar Aug 03 '22 17:08 CardenInsurance

Thanks for the report, the snip is actually relevant here - it dictates what type "var" ends up as. From the error, I can assume it doesn't result in List<PolicyCrop> since that does have a property called count.

i.e.

Dim lstCropRecordsToDelete As List(Of PolicyCrop) = new List(Of PolicyCrop)
If lstCropRecordsToDelete.Count > 0 Then
    ' do stuff
End If

converts to

{
    var lstCropRecordsToDelete = new List<PolicyCrop>();
    if (lstCropRecordsToDelete.Count > 0)
    {
        // do stuff
    }
}

which compiles fine.

Whereas

Dim lstCropRecordsToDelete As List(Of PolicyCrop) = Nothing
If lstCropRecordsToDelete.Count > 0 Then
    ' do stuff
End If

converts to

{
    List<PolicyCrop> lstCropRecordsToDelete = null;
    if (lstCropRecordsToDelete.Count > 0)
    {
        // do stuff
    }
}

which also compiles (because it specified the type since it wasn't the same as what the right hand side would create.

So even if you can't share exactly what's in <snip>, could you let me know what type is inferred for the var, and/or experiment a little with https://icsharpcode.github.io/CodeConverter/ to see what the key part of it is that causes var to appear?

Thanks, Graham,

GrahamTheCoder avatar Aug 04 '22 09:08 GrahamTheCoder

Apologies for leaving out the details. Here's the unmodified VB.NET code:

Dim Live_Data = (From p In db.Policies
               Group Join pc In db.PolicyCrops On pc.Policy_ID Equals p.Policy_ID Into Group1 = Group
               From pc In Group1.DefaultIfEmpty
               Where p.Reinsurance_Year = sf.CombinedSourceFile.Reinsurance_Year_Parsed_From_FileName).ToList

Dim lstCropRecordsToDelete As List(Of PolicyCrop) = (From live In Live_Data
						Where live.pc IsNot Nothing AndAlso (live.pc.IS_DELETED = False And live.p.Reinsurance_Year = sf.CombinedSourceFile.Reinsurance_Year_Parsed_From_FileName)
						Group Join staged In Staged_Data.AsEnumerable On live.pc.ARMtech_Key Equals staged.Field(Of String)("AIP_Insurance_In_Force_Key") Into Group1 = Group
						From row In Group1.DefaultIfEmpty
						Where row Is Nothing
						Select live.pc).ToList

If lstCropRecordsToDelete.Count > 0 Then
	PolicyCrop.Delete_Whole_PolicyCrop(db, lstCropRecordsToDelete.Select(Function(x) x.PolicyCrop_ID).ToArray())
	Get_Modified_Records(db, sf.CombinedSourceFile.File_Modified_Date)
	db.SaveChanges()
End If

and unmodified C# result:


var Live_Data = (from p in db.Policies
                 join pc in db.PolicyCrops on p.Policy_ID equals pc.Policy_ID into Group1
                 from pc in Group1.DefaultIfEmpty()
                 where p.Reinsurance_Year == sf.CombinedSourceFile.Reinsurance_Year_Parsed_From_FileName
                 select p).ToList();

var lstCropRecordsToDelete = (from live in Live_Data
			  where live.pc is not null && live.pc.IS_DELETED == false & live.p.Reinsurance_Year == sf.CombinedSourceFile.Reinsurance_Year_Parsed_From_FileName
			  join staged in Staged_Data.AsEnumerable() on live.pc.ARMtech_Key equals staged.Field<string>("AIP_Insurance_In_Force_Key") into Group1
			  from row in Group1.DefaultIfEmpty()
			  where row is null
			  select live.pc).ToList();

if (lstCropRecordsToDelete.Count > 0)
{
	PolicyCrop.Delete_Whole_PolicyCrop(db, lstCropRecordsToDelete.Select(x => x.PolicyCrop_ID).ToArray());
	Get_Modified_Records(db, sf.CombinedSourceFile.File_Modified_Date);
	db.SaveChanges();
}

This was converted with CodeConverter 9.0.3.0. In the converted result, if I hover over Live_Data, I see List<Policy> (which is wrong, mentioned in #931). Hovering over lstCropRecordsToDelete just gives a question mark ?.

Maybe fixing 931 will fix this issue automatically.

CardenInsurance avatar Aug 04 '22 16:08 CardenInsurance

If all the types are known I think this will work fine. If the compiler can't tell that's a list in the VB snippet, e.g. because it didn't know the type of live.pc, then the converter can't (easily) know either. I'll close this as no fix. If there's a self contained compiling snippet with the same issue though please reopen or create a new issue for it

GrahamTheCoder avatar Oct 11 '22 08:10 GrahamTheCoder