Custom extension method to simplify LINQ to SQL statement

后端 未结 3 1876
粉色の甜心
粉色の甜心 2021-01-23 21:07

I have a snippet of LINQ code that I need to use in a lot of queries

let catchmentId = Convert.ToInt32(
        phy.PhysicalProperty_binData.Substring(offset + 3         


        
相关标签:
3条回答
  • 2021-01-23 21:32

    Wrong language (sorry) here it is in F# if people need.

    let catchmentId =  
        // Curry in invariant components.
        let newSubstr end = 
            phy.PhysicalProperty_binData.Substring(offset + end, 1)
        Convert.ToInt32(
            newSubstr 3 +
            newSubstr 2 + 
            newSubstr 1 +
            newSubstr 0)
    
    0 讨论(0)
  • 2021-01-23 21:39

    You can map custom SQL functions in your Entity model.

    http://msdn.microsoft.com/en-us/library/dd456812(v=vs.103).aspx

    0 讨论(0)
  • 2021-01-23 21:47

    The links on this page will show you how you can use custom attributes to tie your extension method to a user-defined function in your database.

    Of course, that will mean that you need to create a user-defined function in SQL to do the same logic that your extension method wants to do.

    Another option is to have your extension method produce an Expression, and use LINQKit to parse through the query tree, finding the call to .Compile() off of that expression, and inlining that expression in the query.

    0 讨论(0)
提交回复
热议问题