I have the below LINQ query. FactColumns
is a list of FactColumn
objects and SelectedColumns
is a list of strings. I want to get a list of FactColumn
objects using an inner join on SelectedColumns
. However, I am getting null
for below query.
var lst = from fc in this.DataSetFact.FactColumns
开发者_JAVA技巧 join column in m_TableDataDict[tableGuid].SelectedColumns
on fc.Name equals column
select new
{
fc.ColumnType,
fc.DataType,
fc.FriendlyName,
fc.Name,
fc.ParentFactName,
fc.Size,
fc.State
};
Why am I getting null
?
I can see above code running fine. Check if data is loaded properly.
Sample
List<FactColumn> FactColumns = new List<FactColumn>();
List<string> SelectedColumns = new List<string>();
private void Form2_Load(object sender, EventArgs e)
{
FactColumns.Add(new FactColumn() { DataType = "int", Name = "int" });
FactColumns.Add(new FactColumn() { DataType = "string", Name = "string" });
SelectedColumns.Add("string");
var lst = from fc in this.FactColumns join column in SelectedColumns on fc.Name equals column select new { fc.DataType, fc.Name};
foreach (var column in lst)
{
MessageBox.Show(column.Name);
}
}
public class FactColumn
{
public string DataType { get; set; }
public string Name { get; set; }
}
What about this using lambda:
var lst = this.DataSetFact.FactColumns.Where(hm=>hm_TableDataDict[tableGuid].SelectedColumns.Contains(hm.Name)
I believe the problem is with your join. Since you do not seem to be using any information from that I would change it to be a where statement. Try this:
var lst = from fc in this.DataSetFact.FactColumns
where m_TableDataDict[tableGuid].SelectedColumns.Contains(fc.Name)
select new
{
fc.ColumnType,
fc.DataType,
fc.FriendlyName,
fc.Name,
fc.ParentFactName,
fc.Size,
fc.State
};
精彩评论