首页 \ 问答 \ C#lambda - 两个列表中内部列表的差异(除外)(C# lambda - Differences (Except) of inner lists inside two lists)

C#lambda - 两个列表中内部列表的差异(除外)(C# lambda - Differences (Except) of inner lists inside two lists)

我有两个'Table'1 ---- N'Columns列表。 第一个列表包含必须实现的默认架构。 第二个列表包含用户定义的模式。 我需要将第二个列表与第一个列表进行比较,检索架构不匹配的表,以及缺失/未知列的列表。

请考虑以下示例:

public class Table
{
  public string Name {get;set;}
  public IList<Column> Columns {get;set;}
  public Table()
  {
    Columns = new List<Column>();
  }
}
public class Column
{
  public string Name {get;set;}
}
//...
var Default1 = new Table() { Name = "Table1" };
Default1.Columns.Add(new Column() { Name = "X1" });
Default1.Columns.Add(new Column() { Name = "X2" });
var Default2 = new Table() { Name = "Table2" };
Default2.Columns.Add(new Column() { Name = "Y1" });
Default2.Columns.Add(new Column() { Name = "Y2" });

var DefaultSchema = new List<Table>() { Default1, Default2 };

var T1 = new Table() { Name = "Table1" };
T1.Columns.Add(new Column() { Name = "X1" });
var T2 = new Table() { Name = "Table2" };
T2.Columns.Add(new Column() { Name = "Y2" });

var MyTables = new List<Table>() { T1, T2};

/*
var DiffTables = DefaultSchema.Join(??).Select(x => x.Columns).Except(?? MyTables.Select(y => y.Columns) ...
*/

预期结果:

var DiffTables =
 {
  {
    Name = "Table1",
    Columns =
    {
      Name = "X2" //Missing from DefaultSchema.Table1
    }
  },
  {
    Name = "Table2",
    Columns =
    {
      Name = "Y1" //Missing from DefaultSchema.Table2
    }
  }
 }

有没有办法用lamdba表达式,或者只是由master +嵌套的foreach执行此操作?

谢谢!


I have two lists of 'Table' 1 ----N 'Columns. The first list holds the default schema that must be achieved. The second list holds the schema defined by the user. I need to compare the second list against the first one, retrieving the tables where the schema mismatches, also the list of columns missing/unknown.

Consider the following example:

public class Table
{
  public string Name {get;set;}
  public IList<Column> Columns {get;set;}
  public Table()
  {
    Columns = new List<Column>();
  }
}
public class Column
{
  public string Name {get;set;}
}
//...
var Default1 = new Table() { Name = "Table1" };
Default1.Columns.Add(new Column() { Name = "X1" });
Default1.Columns.Add(new Column() { Name = "X2" });
var Default2 = new Table() { Name = "Table2" };
Default2.Columns.Add(new Column() { Name = "Y1" });
Default2.Columns.Add(new Column() { Name = "Y2" });

var DefaultSchema = new List<Table>() { Default1, Default2 };

var T1 = new Table() { Name = "Table1" };
T1.Columns.Add(new Column() { Name = "X1" });
var T2 = new Table() { Name = "Table2" };
T2.Columns.Add(new Column() { Name = "Y2" });

var MyTables = new List<Table>() { T1, T2};

/*
var DiffTables = DefaultSchema.Join(??).Select(x => x.Columns).Except(?? MyTables.Select(y => y.Columns) ...
*/

Expected result:

var DiffTables =
 {
  {
    Name = "Table1",
    Columns =
    {
      Name = "X2" //Missing from DefaultSchema.Table1
    }
  },
  {
    Name = "Table2",
    Columns =
    {
      Name = "Y1" //Missing from DefaultSchema.Table2
    }
  }
 }

Is there any way of doing this with a lamdba expression, or just by a master+nested foreach?

Thanks!


原文:https://stackoverflow.com/questions/34889101
更新时间:2023-11-20 16:11

最满意答案

$(el).remove(selector)有点令人困惑。 它基本上意味着“ 如果匹配selector则从文档中删除el ”。 如果您的jQuery对象具有多个元素并且您要过滤要删除的元素,则这更有意义。

如果您将代码更改为.remove('.modal-login') ,您将看到它删除了整个模态。

你应该使用.find然后.remove

$('.modal-login').find(".alert").remove();
$('.modal-login').prepend(data);

$(el).remove(selector) is somewhat confusing. It basically means "remove el from the document if it matches selector". This makes more sense if your jQuery object has multiple elements and you're filtering which ones to remove.

If you change your code to .remove('.modal-login'), you'll see that it removes the whole modal.

You should use .find then .remove:

$('.modal-login').find(".alert").remove();
$('.modal-login').prepend(data);

相关问答

更多
  • $(el).remove(selector)有点令人困惑。 它基本上意味着“ 如果匹配selector则从文档中删除el ”。 如果您的jQuery对象具有多个元素并且您要过滤要删除的元素,则这更有意义。 如果您将代码更改为.remove('.modal-login') ,您将看到它删除了整个模态。 你应该使用.find然后.remove : $('.modal-login').find(".alert").remove(); $('.modal-login').prepend(data); $(el). ...
  • 在.col-sm-4 div中添加.addScroll div,使用overflow-y元素设置它的高度 .addScroll{ overflow-y:auto; height: 200px; }