English 中文(简体)
linq query(entity framework)中的Cac#方法
原标题:Call c# method in linq query(entity framework)

Is there any chance to run c# method in linq query? I need to do something like that:

//...
class Match {
  public double PrecentageMatching(string s1, string s2) {
    //...
    return result; // value from 0.0 to 1.0
  }
}
//...

string pattern = "nameOfRecord";
var similarRecords = db.Table.Select( 
   r => Match.PrecentageMatching(r.name, pattern) > 0.5 
);

我知道,林克胜地知道有预选材料的方法。 但我不禁要问,有没有办法这样做?

I m using Entity framework. I need to do it without stored procedures and assembly on database side. I dont have access to database.

最佳回答

你们首先需要从数据库中提取数据,然后才进行转变:

string pattern = "nameOfRecord";
var similarRecords = db.Table
                       .Select(r => r.name)
                       .ToList() // This call fetches the data from the DB
                       .Select(x => Match.PrecentageMatching(x, pattern) > 0.5);

这根本不是问题,因为你只是用你的方法改变所交数据。 如果你想使用你的方法 filter 由于你首先需要交回所有数据,并过滤客户的数据,因此,使用<代码>的的回归数据存在问题。 如果表面大,这可能会成为一个问题。

问题回答

改变你的cent 将方法与固定方法相匹配。 然后,它将能够找到匹配。 重点 匹配方法。

而且,你重新做些什么,将回去一片精干的风气。 如果你想从表格中收回记录,你想要“了解”而不是“选择”。

Edit:根据评论,你需要采用ToList()方法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签