Linq 控制台写入线( “Title={0},subcent.Length={1}”, 项目名称、项目、分包合同、长度); } } 公共静态列表筛选器(列表原始、Func筛选器) { 返回original.Where(filter.ToList(); } 公共类表

Linq 控制台写入线( “Title={0},subcent.Length={1}”, 项目名称、项目、分包合同、长度); } } 公共静态列表筛选器(列表原始、Func筛选器) { 返回original.Where(filter.ToList(); } 公共类表,linq,join,Linq,Join,控制台写入线( “Title={0},subcent.Length={1}”, 项目名称、项目、分包合同、长度); } } 公共静态列表筛选器(列表原始、Func筛选器) { 返回original.Where(filter.ToList(); } 公共类表 { 公共int Id{get;set;} 公共字符串标题{get;set;} 公共字符串[]子项{get;set;} } 您似乎想筛选父实体并获取子表关系的计数,对吗?@RubensFarias没错@RubensFarias非常感谢您的快速回

控制台写入线( “Title={0},subcent.Length={1}”, 项目名称、项目、分包合同、长度); } } 公共静态列表筛选器(列表原始、Func筛选器) { 返回original.Where(filter.ToList(); } 公共类表 { 公共int Id{get;set;} 公共字符串标题{get;set;} 公共字符串[]子项{get;set;} }
您似乎想筛选
父实体
并获取
子表
关系的计数,对吗?@RubensFarias没错@RubensFarias非常感谢您的快速回复:-)我认为您的Lambda查询可以正常工作,但正如我之前所说的,参数“key”只有在存在时才会传递到查询中,这就是为什么我在标题中提到“将Linq分为两部分”。似乎您想过滤
父实体
并获取
子表
关系的计数,对吧?@RubensFarias没错@RubensFarias感谢您的快速回复:-)我认为您的Lambda查询可以工作,但正如我之前所说的,参数“key”只有在存在时才会传入查询,这就是为什么我在标题中提到将Linq“分离”为两部分。是的,它确实可以工作!谢谢但是如果我真的想把Linq从一个部分分为两个部分,那该怎么办呢?是的,它确实有效!谢谢但是如果我真的想把Linq从一个部分分为两个部分,那该怎么办呢?还可以吗?再次感谢您的帮助:-)而且您的方法也很有效!TBH,我不知道我是否正确理解了您的问题=/很抱歉误解,我实际上想知道我是否有两个或更多的where子句,如何将它们逐个应用于查询,并在最后将结果选择到一个新对象中。您和@BurnsBA都将所有where子句放在一起并进行了一次“选择”,我选择了两次。我并不是说我是对的,事实上,如果我有机会,我应该按照你的方式去做,只是想知道如果有更多的选择。因为我要用表达,我会给你荣誉,结束这个话题,再次感谢!再次感谢您的帮助:-)您的方式也很有效!TBH,我不知道我是否正确理解了您的问题=/很抱歉误解,我实际上想知道我是否有两个或更多的where子句,如何将它们逐个应用于查询,并在最后将结果选择到一个新对象中。您和@BurnsBA都将所有where子句放在一起并进行了一次“选择”,我选择了两次。我并不是说我是对的,事实上,如果我有机会,我应该按照你的方式去做,只是想知道如果有更多的选择。因为我要用表达,我会给你荣誉,结束这个话题,再次感谢!
var rows = from p in db.P
           join s in db.S on p.Id equals s.ParentId into subContent
           where (some condition here)
           select new{
                Id = p.Id,
                Title = p.Title
                SubContentCount = subContent.Count()
           }
var rows = from p in db.P
           join s in db.S on p.Id equals s.ParentId into subContent
           where (some condition here)
           select p;

if(!string.IsNullOrEmpty(key)){ // I'm using C#
   rows = rows.Where(q => q.Title.Contains(key))
}
var list = rows.Select(q => new ()
           {
                Id = q.Id,
                Title = q.Title,
                subCount = ???.Count()
            });
public class Parent { public int Id {get; set;} public string Title {get; set;} }
public class SubTable { public int Id {get; set;} public int ParentId {get; set;} }
public class Result { public int Id {get; set;} public string Title {get; set;} public int SubContentCount {get; set;} }

var p1 = new Parent() { Id = 1, Title = "Parent_1" };
var p2 = new Parent() { Id = 2, Title = "Parent_2" };
var p3 = new Parent() { Id = 3, Title = "Parent_3" };
var s1_1 = new SubTable() { Id = 11, ParentId = 1 };
var s1_2 = new SubTable() { Id = 12, ParentId = 1 };
var s1_3 = new SubTable() { Id = 13, ParentId = 1 };
var s2_1 = new SubTable() { Id = 21, ParentId = 2 };
var s2_2 = new SubTable() { Id = 22, ParentId = 2 };
var s3_1 = new SubTable() { Id = 31, ParentId = 3 };

var db_P = new List<Parent>() { p1, p2, p3 };
var db_S = new List<SubTable>() { s1_1, s1_2, s1_3, s2_1, s2_2, s3_1 };

public IEnumerable<Result> GetResults(string key = null)
{
    var rows = from p in db_P
               join s in db_S on p.Id equals s.ParentId into subContent
               where string.IsNullOrEmpty(key) || p.Title.Contains(key)
               select new Result() {
                   Id = p.Id,
                   Title = p.Title,
                   SubContentCount = subContent.Count()
               };

    return rows;
}
public static void Main(string[] args)
{
    var rows = new List<Table>
    {
        new Table { Id = 1, Title = "A", SubContent = new [] { "A1" } },
        new Table { Id = 2, Title = "B", SubContent = new [] { "B1", "B2" } },
        new Table { Id = 3, Title = "C", SubContent = new [] { "C1", "C2", "C3" } },
    };

    var title = "C";
    foreach (var item in Filter(rows, table => 
        String.IsNullOrEmpty(title) || table.Title == title))
    {
        Console.WriteLine(
            "Title={0}, SubContent.Length={1}",
            item.Title, item.SubContent.Length);
    }
}

public static List<Table> Filter(List<Table> original, Func<Table, bool> filter)
{
    return original.Where(filter).ToList();
}

public class Table
{
    public int      Id         { get; set; }
    public string   Title      { get; set; }
    public string[] SubContent { get; set; }
}