Linq自加入查询

时间:2011-10-01 05:08:22

标签: c# asp.net linq asp.net-4.0

我需要Linq自我加入的一些帮助。

我有以下课程:

public class Owner
{
public string OwnerId {get;set;}
public string Name {get;set;}
public string Area {get;set;}
public string City {get;set;}
public string Sex {get;set;}
public List<Dog> dog {get;set;}
}

并且表......

ID OwnerId OwnerName TypeId TypeName   TypeValue        TypeCodeId
1     1     John       1     Area      United States       440
2     1     John       2     City      Los-Angeles         221
3     1     John       3     Sex       Female              122
4     2     Mary       1     Area      Mexico              321
4     2     Mary       2     City      Cancun              345
............................................................

我需要以最快的方式将table1的结果解析为所有者列表。 注意:Type可以为null,但我仍然需要显示该所有者(所以,我假设左连接应该有效)。

这是我的所作所为。 (所有者是包含table1结果的Web服务类)

public IEnumerable<Owner> GetOwners() { 
  return  (from owner in owners 
          join area in owners into owner_area
          from oa in owner_area.DefaultIfEmpty()
          join City in owners into owner_city
          from oc in owner_city.DefaultIfEmpty()
          join sex  in owners into owner_sex
          from os in owner_sex.DefaultIfEmpty()
          where oa.TypeId == 1 && oc.TypeId ==2 && os.TypeId ==3
          select new Owner() {OwnerId = owner.OwnerId, 
                              Name = owner.Name,
                              Area = oa.TypeValue,
                              City = oc.TypeValue,
                              Sex = os.TypeValue}).Distinct(); 
}

此查询有几个问题:

  1. 它返回多个结果,而distinct不会按预期工作
  2. 我曾尝试使用GroupBy,但它说不能隐式将所有者转换为IEnumerable <int, Owner>
  3. 超级慢
  4. 如何通过自我加入获得独特的记录并提高性能? 感谢

    更新 谢谢你们为你的ansewers,现在测试,但我发现我忘了再提供一件东西了。我在表格布局中添加了一个名为TypeCodeId的新列(参见上文) 用户可以根据他们的选择过滤值。所以,我有区域,城市和性别的TypeCodeId + TypeValue词典。所有这些参数都是可选的(如果用户没有选择任何参数,我只会向他们显示所有记录。

    因此,假设用户已选择过滤器Area: Unites States并过滤City: Los Angeles

    他们我的查询看起来像这样:

    Select Projects where Area equals United States(440) and City equals Los Angeles(221)
    

    如果选择了“仅区域:墨西哥”,那么我的查询将会显示如下:

    Select Projects where Area equals Mexico(321)
    

    我不确定如何使用您在示例中提供的内容中的可选where子句。

2 个答案:

答案 0 :(得分:3)

由于表未规范化,我们需要从对象/表中获取distict用户。这可以通过以下方式完成:

owners.Select(o => new { o.OwnerId, o.OwnerName }).Distinct()

然后我们需要使用两个匹配值来加入“类型”,一个用于ownerId,另一个用于特定类型。

var ownerQuery =
    from o in owners.Select(o => new { o.OwnerId, o.OwnerName }).Distinct()

    join area in owners on new { o.OwnerId, TypeId = 1 } equals new { area.OwnerId, area.TypeId } into areas
    from area in areas.DefaultIfEmpty()

    join city in owners on new { o.OwnerId, TypeId = 2 } equals new { city.OwnerId, city.TypeId } into cities
    from city in cities.DefaultIfEmpty()

    join sex in owners on new { o.OwnerId, TypeId = 3 } equals new { sex.OwnerId, sex.TypeId } into sexes
    from sex in sexes.DefaultIfEmpty()

    select new 
        { 
            owner = o,
            Area = (area != null) ? area.TypeValue : null, 
            City = (city != null) ? city.TypeValue : null, 
            Sex = (sex != null) ? sex.TypeValue : null, 
        };

您可能需要更改上例中的投影。

答案 1 :(得分:2)

为了获得最佳性能,请认为这是实现这一目标的方法。

public IEnumerable<Owner> GetOwners(IEnumerable<Tuple<int, int>> filter)
{
    var q = (from o in owners
            join f in filter on 
               new {o.TypeId, o.TypeCodeId} equals 
               new {TypeId = f.Item1, TypeCodeId = f.Item2}
            select o).ToList();

    var dic = q.ToDictionary (o => new {o.OwnerId, o.TypeId}, o => o.TypeValue);
    foreach (var o in q.Select(o => new { o.OwnerId, o.OwnerName }).Distinct())
    {
        var owner = new Owner()
        {
            OwnerId = o.OwnerId,
            Name = o.OwnerName
        };
        string lookup;
        if(dic.TryGetValue(new {o.OwnerId, TypeId = 1}, out lookup))
            owner.Area = lookup;
        if(dic.TryGetValue(new {o.OwnerId, TypeId = 2}, out lookup))
            owner.City = lookup;
        if(dic.TryGetValue(new {o.OwnerId, TypeId = 3}, out lookup))
            owner.Sex = lookup;

        yield return owner;
    }
}

为了获得更高的性能,您可以编写一个IEqualityComparer类,仅比较int OwnerId并将其发送到Distinct函数