开发者

LINQ-to-SQL Generic GetByIDs method

I'm currently using this to get an object by it's primary key value.

I'm trying to find a way to create a similar method GetByIDs where I can pass an IEnumerable(of object) and do ids.contains(pk), but there's no Contains expression.

Anyone know how I would do this?

Public Function GetByID(Of T As Class)(ByVal pk As Object) As T
    Dim itemParam = Expression.Parameter(GetType(T), "item")
    Return GetTable(Of T).Single(
        Expression.Lambda(Of Func(Of T, Boolean))(
            Expression.Equal(
                Expression.Property(itemParam, GetPrimaryKeyName(Of T)),
                Expression.Constant(pk)
                ),
            New ParameterExpression() {itemParam}
            )
        )
End Function

Public Function GetPrimaryKeyName(Of开发者_如何学JAVA T)() As String
    Return Mapping.GetTable(GetType(T)).RowType.IdentityMembers(0).Name
End Function


You need to use Expression.Call

Update - different overload

Expression.Call(typeof(Enumerable),
                "Contains",
                new Type[] { Expression.Constant(...).Type }
                Expression.Property(...),
                Expression.Constant(...));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜