0

我正在研究一个让我的大脑融化的问题,尽管我认为它不应该这么难。我的例子很长,所以我会尽量让我的问题简短!

我有一个数组对象,其中包含一些也是数组的元素。例如:

customerAddresses = new customer_address[]
{
  new    // address #1
  {
    customer_id = 6676979,
    customer_address_seq = 1,
    customer_address_match_codes = new []
    {
      new
      {
        customer_address_seq = 1,
        customer_id = 6676979,
        customer_match_code_id = 5
      }
    }
  },
  new    // address #2
  {
    customer_id = 6677070,
    customer_address_seq = 1,
    customer_address_match_codes = new []
    {
      new
      {
        customer_address_seq = 1,
        customer_id = 6677070,
        customer_match_code_id = 4
      },
      new
      {
        customer_address_seq = 1,
        customer_id = 6677070,
        customer_match_code_id = 5
      },
      new
      {
        customer_address_seq = 1,
        customer_id = 6677070,
        customer_match_code_id = 3
      }
    }
  },
  new    // address #3
  {
    customer_id = 6677070,
    customer_address_seq = 2,
    customer_address_match_code = new []
    {
      new
      {
        customer_address_seq = 2,
        customer_id = 6677070,
        customer_match_code_id = 4
      },
      new
      {
        customer_address_seq = 2,
        customer_id = 6677070,
        customer_match_code_id = 5
      }
    }
  }
};

如您所见,数组包含许多地址记录,每个customer_id和的组合都有一条记录customer_address_seq。我要做的是customer_address根据以下规则找到最佳匹配:

  • 必须customer_match_code_id等于 4 并且必须有 1 等于 5
  • 如果有一个customer_match_code_id等于 3,则认为这是customer_address一个更强的匹配。

根据上述规则,第二个customer_address元素是“最佳匹配”。然而,这个问题的最后一点复杂性是可能有多个“最佳匹配”。我需要如何处理这种情况是通过customer_address记录 minimumcustomer_id和 minimum customer_address_seq

我在想使用 LINQ 将是我最好的选择,但我对它的经验不够,所以我只是不停地旋转我的轮子。

4

4 回答 4

0

未经测试且名称不同,但这应该可以帮助您

            customer cb = null;
            customer[] cs = new customer[] {new customer()};

            foreach (customer c in cs.OrderBy(x => x.id).ThenBy(y => y.seq))
            {
                if(c.addrs.Any(x => x.num == "5"))
                {
                    if(c.addrs.Any(x => x.num == "3"))
                    {
                        if (cb == null) cb = c;
                        if (c.addrs.Any(x => x.num == "2"))
                        {
                            cb = c;
                            break;
                        }
                    }
                }
            }
于 2012-07-11T22:36:47.173 回答
0

在 LINQ 中似乎很简单:

var query =
    from ca in customerAddresses
    where ca.customer_address_match_codes.Any(
        mc => mc.customer_match_code_id == 4)
    where ca.customer_address_match_codes.Any(
        mc => mc.customer_match_code_id == 5)
    orderby ca.customer_id
    orderby ca.customer_address_seq
    orderby ca.customer_address_match_codes.Any(
        mc => mc.customer_match_code_id == 3) descending
    select ca;

var result = query.Take(1);

看起来怎么样?

于 2012-07-12T00:13:22.070 回答
0

必须对您的班级进行更改,以便您实际上将您的一个集合分配给某些东西:

customer_address_match_codes = new customer_address_match_code[]
{
  new
  {
    customer_address_seq = 1,
    customer_id = 6676979,
    customer_match_code_id = 5
  }
}

然后这是我测试过的 LINQ,它按照您的要求执行:

var result = (from c in customerAddresses
    let isMatch = c.customer_address_match_codes
                     .Where (cu => cu.customer_match_code_id == 4).Any () &&
                  c.customer_address_match_codes
                     .Where (cu => cu.customer_match_code_id == 5).Any ()
    let betterMatch = isMatch && c.customer_address_match_codes
                  .Where (cu => cu.customer_match_code_id == 3).Any () ? 1 : 0
    where isMatch == true
    orderby betterMatch descending, c.customer_id, c.customer_address_seq
    select c)
    .FirstOrDefault ();

我在这里使用匿名类型的数据编写了一个示例:http: //ideone.com/wyteM

于 2012-07-12T00:13:39.833 回答
0

这听起来像是 LINQ 的工作

var bestMatch = (from address in DATA
where address.customer_address_match_code.Any(
  x => x.customer_match_code_id == 4)
where address.customer_address_match_code.Any(
  x => x.customer_match_code_id == 5)
select address).OrderBy(
  x => x.customer_address_match_code.Where(
    y => y.customer_match_code_id >= 3)
  .OrderBy(y => y.customer_match_code_id)
  .First()
  .customer_match_code_id).FirstOrDefault();

我的理论是这样的:选择同时具有 customer_match_code_id == 4 和 customer_match_code_id == 5 的地址。然后按至少为 3 的最低 customer_match_code_id 对它们进行排序,然后取第一个。如果有一个 customer_match_code_id 等于 3,则选择那个,如果没有,则选择其他一些。如果 4 和 5 均不匹配,则返回 null。

未经测试。

于 2012-07-11T23:25:20.987 回答