使用两个列表中的LINQ构建列表

时间:2015-02-14 22:14:50

标签: linq list

我有两个清单:

List1 = {'A','B','C','D','E','F','G','H','I','J','K'}

List2 = {'B','C','D'}

List1是表​​中的所有列,List2是构成同一个表上的外键或主键的列。

在此示例中,“A”,“B”,“C”和“D”是表的主键。 'B','C'和'D'是同一个表的外键。 'G'和'H'是我不想包含的表格的两列。

我似乎无法编写产生这个的LINQ语句:

List3 = {'B','C','D','E','F','I','J','K'}

List3是我想要给我生成的“UPDATE”存储过程的参数列表。 List2 +'K'将是我的“WHERE”; 'E','F','I'和'J'将成为程序在“SET”中使用的列。

每次执行LINQ时,List1和List2都会不同。

所有列表都是List类型,其中DbColumn定义为:

public class DbColumn
{
    public string Name { get; set; }
    //public bool IsPrimaryKey { get; set; }
    //public bool IsForeignKey { get; set; }
    public int OrdinalPosition { get; set; }
    public string DataType { get; set; }
    public int CharMaxLength { get; set; }
    public bool AllowNulls { get; set; }

    public static DbColumn Create(string name, int ordinal_position, string data_type, int charMaxLength, string is_nullable)
    {
        DbColumn column = new DbColumn();

        column.Name = name;
        //column.IsPrimaryKey = isPk;
        //column.IsForeignKey = isFk;
        column.OrdinalPosition = ordinal_position;
        column.DataType = data_type;
        column.CharMaxLength = charMaxLength;

        if (!String.IsNullOrEmpty(is_nullable))
            column.AllowNulls = is_nullable.ToUpper() == "YES" ? true : false;
        else
            column.AllowNulls = false;

        return column;
    }

    public override string ToString()
    {
        return Name;
    }
}

列可以是DbConstraint和DbTable的一部分。 DbTable还将DbConstraint作为属性:

public class DbTable
{
    public string Name { get; set; }
    public string Catalog { get; set; }
    public string Schema { get; set; }
    public List<DbColumn> Columns { get; set; }
    public DbSProc Insert { get; set; }
    public DbSProc Update { get; set; }
    public DbSProc Delete { get; set; }
    public List<DbSProc> ReadProcedures { get; set; }
    public List<DbConstraint> Constraints { get; set; }

    public static DbTable Create(string name, string catalog, string schema, List<DbColumn> columns)
    {
        DbTable table = new DbTable();

        table.Catalog = catalog;
        table.Schema = schema;
        table.Name = name;
        table.Columns = columns;
        table.ReadProcedures = new List<DbSProc>();

        return table;
    }

    public override string ToString()
    {
        return Name;
    }
}

public class DbConstraint
{
    public string Name { get; set; }
    public bool IsPrimary { get; set; }
    public List<DbColumn> Columns { get; set; }

    public static DbConstraint Create(string name, bool isPrimary)
    {
        DbConstraint constraint = new DbConstraint();
        constraint.Name = name;
        constraint.IsPrimary = isPrimary;
        constraint.Columns = new List<DbColumn>();

        return constraint;
    }

    public override string ToString()
    {
        return Name;
    }
}

LINQ语句的目标是仅为参数选择“WHERE”片段所需的列(例如:外键/主键加“_VerCol”)和“SET”片段所需的列(非 - 主要钥匙)。

您可以在CodePlex上找到代码:https://sqlmeth.codeplex.com

有人可以帮帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:-1)

我假设您知道要排除的字符。

List<char> a = new List<char>() { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' };
List<char> b = new List<char>() { 'B', 'C', 'D' };
List<char> exclusionList = new List<char> {'A','G', 'H'};

List<char> c = a.Select(x => x).Where(x => !exclusionList.Contains(x)).ToList();