如何在linq join中调用自定义扩展方法 [英] how to call custom extension method in linq join

查看:159
本文介绍了如何在linq join中调用自定义扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有两张桌子员工和部门。我需要在电话号码上加入内心。这里的问题是员工表包含的电话号码如'1234567890'(10)。但在部门表格中,如果一个人有三个电话号码,则会像1234567890234567890212345654321那样存储。不连续的字符串表示没有逗号,没有空格。我需要比较部门表电话号码的每10位数与员工表电话号码。我怎么能得到它。

提前致谢。

员工

id电话号码

1 1234567890



部门

dept_id电话号码

30 12345678963910453678


$ b来自员工的
$ b加入部门的部门

on emp.phoneno等于dept.phoneno



PLZ帮助



谢谢,

Bony

Hi,

I have two tables employee and department. I need to innerjoin on phone numbers. Here the problem is Employee table contains phone nos like '1234567890' (10) digits. But in department table if a person has three phone numbers they are stored like '1234567890234567890212345654321' like that .continuous string representation no commas,no spaces. I need to compare on every 10 digits of department table phone no with employee table phone no. How Can I get it.
Thanks in advance.
Employee
id phone no
1 1234567890

department
dept_id Phone no
30 12345678963910453678

from emp in employee
join dept in department
on emp.phoneno equals dept.phoneno

PLZ help

Thanks,
Bony

推荐答案

如果你是实际上使用数据库我强烈建议使用一个单独的电话号码表,其中包含适当的外键给员工,部门或两者,具体取决于要求。



但是如果这样在某种其他情况下,您可以考虑以下内容:

If you're actually using a database I strongly suggest using a separate table for phone numbers with proper foreign keys to employee, department, or both, depending on the requirements.

However if this is some other kind of situation, you can consider something like the following:
public static class Splitter {
   public static List<string> SplitByLength(this string theString, int length) {
      List<string> returnList = new List<string>();

      try {
         for (int counter = 0; counter < theString.Length; counter += length) {
            returnList.Add(theString.Substring(counter, length));
         }
      } catch (System.Exception exception) {
         MessageBox.Show(exception.Message, "Now this was a surprise");
      }

      return returnList;
   }
}</string></string></string>



和使用示例


And the usage example

List<string> a_list = new List<string>();
List<string> b_list = new List<string>();

a_list.Add("1234567890");
a_list.Add("3910453678");

b_list.Add("12345678963910453678");
b_list.Add("123456789023456789021234565432");

var q1 = from item1 in a_list
         join item2 in b_list
         on 1 equals 1
         where item2.SplitByLength(10).Contains(item1)
         select new {
            i1 = item1,
            i2 = item2
         };

foreach (var a1 in q1) { }</string></string></string></string>



但是,如果这是一个您正在使用的数据库,例如使用Entity Framework,上面的查询将表现得非常糟糕,例如来自性能观点。


However, as said if this is a database you're using for example with Entity Framework, the query above will perform extremely badly for example from performance point of view.


这篇关于如何在linq join中调用自定义扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆