开发者

How do i use linq to do this?

开发者 https://www.devze.com 2023-03-13 00:31 出处:网络
foreach (DataRow row in dtcols.Rows) { if (!avlcols.Exists(x => x.Col开发者_StackOverflow社区Name.ToLower() == row[\"field_name\"].ToString().ToLower()))
foreach (DataRow row in dtcols.Rows)
{
    if (!avlcols.Exists(x => x.Col开发者_StackOverflow社区Name.ToLower() == row["field_name"].ToString().ToLower()))
    {
        avlcols.Add(new Column() 
            {
                ColName=row["field_name"].ToString(),
                ColWidth=row["field_width"].ToString() 
            });
    }
}

Adding only columns that dont exist in avlcols.


Change your if statment to

!avlcols.Contains(x => x.ColName.ToLower() == row["field_name"].ToString().ToLower()))

May I also Suggest using syntax like this instead

!avlcols.Contains(x => String.Equals(x.ColName, row["field_name"].ToString(), OrdinalIgnoreCase))


Replace avlCols.Exists with avlCols.Contains and you're there.


var rows = from row in dtcols.Cast<DataRow>()
           where !avlcols.Exists(x => x.ColName.Equals(row["field_name"].ToString(),StringComparison.OrdinalIgnoreCase))
           select row;
foreach(var row in rows)
{
    avlcols.Add(new Column()
    {
        ColName = row["field_name"].ToString(),
        ColWidth = row["field_width"].ToString()
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消