C# C中的LINQ查询?

C# C中的LINQ查询?,c#,linq,C#,Linq,我是C和Linq编程界的新手。 我想做一些类似的事情,但子类型是FK to table类型,我不能做我在本例中所做的事情: public static List<DropdownModel> GetSubTypes(List<string> ListTypes) { List<DropdownModel> SubTypes = new List<DropdownModel>(); using (Documen

我是C和Linq编程界的新手。 我想做一些类似的事情,但子类型是FK to table类型,我不能做我在本例中所做的事情:

public static List<DropdownModel> GetSubTypes(List<string> ListTypes)
    {
        List<DropdownModel> SubTypes = new List<DropdownModel>();
        using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
        {
            foreach (string TypeID in ListTypes)
            {
                int TypeIDINT = Int32.Parse(TypeID);
                SubTypes.AddRange((from C in DataBase.SubType.Where(s => s.Active && s.TypeID == TypeIDINT)
                            select new DropdownModel()
                            {
                                ID = C.SubTypeID,
                                Description = C.Name,
                                Selected = false
                            }).ToList());
            }
        }
        return SubTypes;
    }
但我不知道如何将其转换为linq查询并执行Type.AddRange


有人能帮我吗?

您可以编写一个类似于编写sql的连接查询

from C in DataBase.Type
join s in DataBase.SubType.Where(s => s.Active && s.SubTypeId == SubTypeIDINT) on C.TypeID equals s.TypeID
select new DropdownModel()
    {
        ID = C.TypeID,
        Description = C.Name,
    }
可以使用Intersect方法从提供的子类型列表中查找包含任何子类型的类型。这还消除了使用foreach进行迭代的需要,并将其留给Linq处理

List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();
DataBase.Type.Where(s => s.SubType.Select(st => st.SubTypesID).Intersect(subTypes).Any())
下面是一个基于您的代码的示例

public static List<DropdownModel> GetTypesBySubTypes(List<string> ListSubTypes)
    {
        List<DropdownModel> Types = new List<DropdownModel>();
        List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();

        using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
        {
                Types.AddRange((from C in DataBase.Type
                               .Where(s => s.Active 
                                       && subTypes.Intersect(s.SubType.Select(st => st.SubTypesID)).Any())
                                select new DropdownModel()
                                {
                                    ID = C.TypeID,
                                    Description = C.Name,
                                }).ToList());

        }
        return Types;
    }

HTH

尝试以下操作:var query=from t in DataBase.SubType.where x=>x.subfid==3 on t.TypeID equal st.TypeID选择new{Type=t,SubType=st}。tolist出于性能原因,我选择此作为已接受的答案,谢谢!
List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();
DataBase.Type.Where(s => s.SubType.Select(st => st.SubTypesID).Intersect(subTypes).Any())
public static List<DropdownModel> GetTypesBySubTypes(List<string> ListSubTypes)
    {
        List<DropdownModel> Types = new List<DropdownModel>();
        List<int> subTypes = ListSubTypes.Select(s => int.Parse(s)).ToList();

        using (DocumentXtractorEntities DataBase = new DocumentXtractorEntities())
        {
                Types.AddRange((from C in DataBase.Type
                               .Where(s => s.Active 
                                       && subTypes.Intersect(s.SubType.Select(st => st.SubTypesID)).Any())
                                select new DropdownModel()
                                {
                                    ID = C.TypeID,
                                    Description = C.Name,
                                }).ToList());

        }
        return Types;
    }