質問

I have this linq to sql query i want to convert to linq to entities

Dim db As New DataClasses1DataContext()
Dim bedspace = (From bed In db.Beds Where SqlMethods.Like(bed.BedName,"ward%")
                Select Convert.ToInt32(bed.BedName.Substring(4))).ToList()

If bedspace.Count <> 0 Then
    txtProvisonalDiagnosis.Text = bedspace.Max
Else
    txtProvisonalDiagnosis.Text = "try again"
End If

The query works but when trying to implement the same thing with entities like this

Dim bedspace = From bed In HMS.Beds Where bed.BedName.Contains("ward")
                    Select Convert.ToInt32(bed.BedName.Substring(4).ToString)

If bedspace.Count <> 0 Then
    txtProvisonalDiagnosis.Text = bedspace.Max
Else
    txtProvisonalDiagnosis.Text = "am still a sucker"
End If

The compiler complains that LINQ to Entities does not recognize the method 'Int32. ToInt32(System.String)' method, and this method cannot be translated into a store expression. Please help me out.

役に立ちましたか?

解決 2

Try something like this, i dont have a compiler nearby but this cant be far off

Dim bedspace as generic.list(of integer) = (From bed In HMS.Beds Where bed.BedName.Contains("ward")
                Select bed.BedName.Substring(4).ToString).tolist

If bedspace.Count <> 0 Then
    txtProvisonalDiagnosis.Text = bedspace.Max
Else
    txtProvisonalDiagnosis.Text = "am still a sucker"
End If

他のヒント

The database does not know of the function Convert.ToInt32, but you can use AsEnumerable() to move the data into memory and do the conversion there instead. If I remember my VB correctly (which I quite possibly don't), this should do it;

Dim bedspace = 
  (From bed In HMS.Beds Where bed.BedName.Contains("ward") Select bed.Bedname)
   .AsEnumerable()
   .Select(Function(bedname) Convert.ToInt32(bedname.Substring(4).ToString))

I simply did this and the compiler did not complain again.

  Dim bedspace = From bed In HMS.Beds Where bed.BedName.Contains("ward")
                    Select CType(bed.BedName.Substring(4), Integer)



    If bedspace.Count <> 0 Then
        txtProvisonalDiagnosis.Text = bedspace.Max
    Else
        txtProvisonalDiagnosis.Text = "am still a sucker"
    End If
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top