The problem with my inner join

I have the following LINQ query. FactColumnsis a list of objects FactColumn, and SelectedColumnsis a list of strings. I want to get a list of objects FactColumnusing an inner join on SelectedColumns. However, I get nullfor the request below.

var lst = from fc in this.DataSetFact.FactColumns
          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?

+3
source share
2 answers

I see that the code is working fine. Check if the data is loading correctly.

Example

    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; }
    }

How about this using lambda:

var lst = this.DataSetFact.FactColumns.Where(hm=>hm_TableDataDict[tableGuid].SelectedColumns.Contains(hm.Name)
+1
source

I believe the problem is with your connection. Since you do not seem to use any of the information from this, I would modify it as a where statement. Try the following:

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
      };
0
source

Source: https://habr.com/ru/post/1790415/


All Articles