Calling a method inside a Linq query

后端 未结 3 1143
攒了一身酷
攒了一身酷 2020-12-04 19:55

I want to insert into my table a column named \'S\' that will get some string value based on a value it gets from a table column.

For example: for each ID (a.z

3条回答
  •  天命终不由人
    2020-12-04 20:04

    You'll want to split it up into two statements. Return the results from the query (which is what will hit the database), and then enumerate the results a second time in a separate step to transform the translation into the new object list. This second "query" won't hit the database, so you'll be able to use the someMethod() inside it.

    Linq-to-Entities is a bit of a strange thing, because it makes the transition to querying the database from C# extremely seamless: but you always have to remind yourself, "This C# is going to get translated into some SQL." And as a result, you have to ask yourself, "Can all this C# actually get executed as SQL?" If it can't - if you're calling someMethod() inside it - your query is going to have problems. And the usual solution is to split it up.

    (The other answer from @BrokenGlass, using .AsEnumerable(), is basically another way to do just that.)

提交回复
热议问题