这个源代码中的c参数是什么?

时间:2016-01-25 13:24:52

标签: c# linq linq-to-sql

我想知道下面的源代码中的c是什么。你能解释一下我在做什么吗?

        private void txtFamilytoSearch_TextChanged(object sender, EventArgs e)
    {
        var db = new LINQDataContext();
        if (txtFamilytoSearch.Text == "")
            gvTable.DataSource = db.MyTables;
        else
            gvTable.DataSource = db.MyTables.Where(c => c.Family.Substring(0, txtFamilytoSearch.Text.Length) == txtFamilytoSearch.Text).Select(c => c);

    }

这是linq技术中C#代码的一部分。

谢谢; - )

2 个答案:

答案 0 :(得分:2)

当您使用Linq where时,它会迭代集合中的每个项目并应用您提供的谓词。

在这种情况下,c代表集合db.MyTables

中的单个元素

答案 1 :(得分:1)

它是集合中当前迭代元素的标识符。这和你写的一样

var result = new List<T>;  // here T is the actual type of your list 

foreach(var c in db.MyTables) 
{ 
    if (c.Family.Substring(0, txtFamilytoSearch.Text.Length) == txtFamilytoSearch.Text) 
    { 
        result.Add(c); 
    }
}
相关问题