locked
can LINQ do this? RRS feed

  • Question

  • User-1141772636 posted

    I have a LINQ query - its a good query and doesn't cause problems but now I am trying to ask it to do something funky and well - its not working.


    I have a list of codes that I want this query to search for.  I'm just pulling the list of codes from the web.config so it doesn't have to be an actual list type - just whatever will/might work.  This list of codes will be passed into a function containing this query (for example List<string>, but that type is not required)

    var query = from c in DataSet1.Table1.AsEnumerable()
                     where c.code == (insert list of codes here)
                     select new { insert fields here }

    I didn't really expect it to work, but am having a hard time trying to wrap my head around how I can do this with LINQ.


    Should I try to build a class that loads all my codes and then try to run a query between the DataTable and class??????

    Friday, March 12, 2010 1:36 PM

Answers

  • User626880745 posted

    yes, Linq can do something like that. You could take a List<string> or List<CustomType> but of course the latter would introduce complexity and not recommeded until you can get by with a simple implementation.


    here's a sample that you can follow:

                DataTable dt1 = new DataTable();
                dt1.Columns.Add(new DataColumn("ID", typeof(Int32)));
                dt1.Columns.Add(new DataColumn("Field1", typeof(string)));
    
                DataRow row = null;
                for (int i = 0; i < 5; i++)
                {
                    row = dt1.NewRow();
                    row[0] = i;
                    row[1] = "F" + i.ToString();
                    dt1.Rows.Add(row);
                }
    
    
                List<string> list = new List<string>();
                list.Add("F3");
                list.Add("F5");
    
                var query = from q in dt1.AsEnumerable()
                            where list.Contains(q.Field<String>("Field1"))
                            select new
                            {
                                ID = q.Field<Int32>("ID"),
                                Field1 = q.Field<String>("Field1")
                            };
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 6:51 PM

All replies

  • User626880745 posted

    yes, Linq can do something like that. You could take a List<string> or List<CustomType> but of course the latter would introduce complexity and not recommeded until you can get by with a simple implementation.


    here's a sample that you can follow:

                DataTable dt1 = new DataTable();
                dt1.Columns.Add(new DataColumn("ID", typeof(Int32)));
                dt1.Columns.Add(new DataColumn("Field1", typeof(string)));
    
                DataRow row = null;
                for (int i = 0; i < 5; i++)
                {
                    row = dt1.NewRow();
                    row[0] = i;
                    row[1] = "F" + i.ToString();
                    dt1.Rows.Add(row);
                }
    
    
                List<string> list = new List<string>();
                list.Add("F3");
                list.Add("F5");
    
                var query = from q in dt1.AsEnumerable()
                            where list.Contains(q.Field<String>("Field1"))
                            select new
                            {
                                ID = q.Field<Int32>("ID"),
                                Field1 = q.Field<String>("Field1")
                            };
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 6:51 PM
  • User-1141772636 posted

    Sorry for the late reply, but that works very well.  Thanks!!!

    Thursday, June 9, 2011 11:23 AM