如何使用列表查询查找表?

时间:2016-12-01 17:28:49

标签: c# linq

鉴于以下类,我希望能够使用ID列表返回DesignAttribute表中AttributeId为1或3的设计。

public class Design
{
    public int DesignId { get; set; }
    public string DesignName { get; set; }
    public virtual List<DesignAttribute> DesignAttributes { get; set;}
}

public class Attribute
{
    public int AttributeId { get; set; }
    public string AttributeName { get; set; }
}

public class DesignAttribute
{
    public int DesignAttributeId { get; set; }
    public virtual Design Design { get; set; }
    public virtual Attribute Attribute { get; set; }
}

设计可以包含1个或多个属性,例如

设计表

DesignId    DesignName
1           Design A
2           Design B
3           Design C

属性表

AttributeId     AttributeName
1               Light
2               Dark
3               Demo

DesignAttribute表

DesignAttributeId   Design_DesignId     Attribute_AttributeId
1                   1                   1               Design A is Light 
2                   1                   3               Design A is also a Demo 
3                   2                   2               Design B is Dark
4                   3                   1               Design C is Light

我有以下代码

//attributes list = "[1,3]  I want any designs that have  Light OR Demo attributes"
public List<Design> FilterDesigns(List<string> attributes)
{
    //sudo code as i'm not sure how to structure this.
    var designs = db.Designs.Where(i => i.DesignAttributes 
    WHERE     DesignAttributes.AttributeId is in the list of attributes passed into the method) 
}

所以我希望最终得到包含设计A和设计C设计的2个项目的列表,因为它们都具有针对DesignAttribute查找表中的属性ID 1和3的ID。

3 个答案:

答案 0 :(得分:2)

试试这个:

var designs = db.Designs.Where(design => 
                  design.DesignAttributes.Any(designAttribute => 
                   attributes.Contains(designAttribute.Attribute.AttributeId)))
              .ToList();

答案 1 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication29
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Design> designTable = new List<Design>() {
                new Design() { DesignId = 1,  DesignName = "A"},
                new Design() { DesignId = 2,  DesignName = "B"},
                new Design() { DesignId = 3,  DesignName = "C"}
            };

            List<Attribute> attributeTable = new List<Attribute>() {
                new Attribute() { AttributeId = 1, AttributeName = "Light"},
                new Attribute() { AttributeId = 2, AttributeName = "Dark"},
                new Attribute() { AttributeId = 3, AttributeName = "Demo"}
            };
            List<DesignAttribute> designAttributeTable = new List<DesignAttribute>() {
                new DesignAttribute() { DesignAttributeId = 1, DesignId = 1, AttributeId = 1},
                new DesignAttribute() { DesignAttributeId = 2, DesignId = 1, AttributeId = 3},
                new DesignAttribute() { DesignAttributeId = 3, DesignId = 2, AttributeId = 2},
                new DesignAttribute() { DesignAttributeId = 4, DesignId = 3, AttributeId = 1}
            };

            var results = (from dattbl in designAttributeTable
                           join dttbl in designTable on dattbl.DesignId equals dttbl.DesignId
                           join attbl in attributeTable on dattbl.AttributeId equals attbl.AttributeId
                           select new { designName = dttbl.DesignName, attributeName = attbl.AttributeName }).ToList();


        }

    }
    public class Design
    {
        public int DesignId { get; set; }
        public string DesignName { get; set; }
        public virtual List<DesignAttribute> DesignAttributes { get; set; }
    }

    public class Attribute
    {
        public int AttributeId { get; set; }
        public string AttributeName { get; set; }
    }

    public class DesignAttribute
    {
        public int DesignAttributeId { get; set; }
        public int DesignId { get; set; }
        public int AttributeId { get; set; }
    }

}

答案 2 :(得分:1)

您可以尝试使用此查询:

103463K - 3103K = 100360K

它将查询 var ids = new List<int> { 1, 3 }; var designs = db.DesignAttributes .Where(m => ids.Contains(m.DesignAttributeId)) .Select(p => p.Design) .ToList(); 列表DesignAttributesDesignAttributeId的位置ids。而不是选择Designs