Can I populate a class from LINQ query?

anderdw2
anderdw2 used Ask the Experts™
on
I have a LINQ query on a csv reader.  I have a class that I would like to populate with the query results.

I can't pass the class in the creation, like commented out below asmy csv has multiple types of data.  So, I look at column 4 to see which type it is and I'd like to create a class from that query, then do another query for each different column 4.  Each would point to a new class after the query.

//TextFileReader<CornSilage> csvReader = new TextFileReader<CornSilage>(thisFile, ",");
TextFileReader csvReader = new TextFileReader(thisFile, ",");
                            //TextFileReader<CornSilage> csvReader = new TextFileReader<CornSilage>(thisFile, ",");
                            var thisQuery = from it2 in csvReader
                                            where it2[4].Equals("Corn Silage")
                                            select new CornSilage {};
                            foreach (var x2 in thisQuery)
                            {
                                    
                            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can populate classes from linq queries like this:
IEnumerable<Person> people = from p in someList select new Person() { Name = p.SomeField };

Open in new window


That will return a list (IEnumerable).  I don't understand exactly what your query is doing, but hopefully you can see how to cast with this.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
What if you did something like a Factory pattern, where you have a common interface and specialized classes that implement that interface. Then you can pass in your string and get back the proper class. For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _27245238
{
    class Program
    {
        static void Main(string[] args)
        {
            TextFileReader csvReader = new TextFileReader(thisFile, ",");
            var thisQuery = from it2 in csvReader
                            select NameYourFactory.Create(it2[4]);
        }
    }

    public static class NameYourFactory
    {
        public static IPickAName Create(string type)
        {
            switch (type)
            {
                case "Corn Silage":
                    return new CornSilage();
                case "Second Class":
                    return new SecondClass();
                case "Third Class":
                    return new ThirdClass();
                case "Fourth Class":
                    return new FourthClass();
                default:
                    throw new ArgumentException("Type not recognized.");
            }
        }
    }

    public interface IPickAName
    {

    }

    public class CornSilage : IPickAName
    {

    }

    public class SecondClass : IPickAName
    {

    }

    public class ThirdClass : IPickAName
    {

    }

    public class FourthClass : IPickAName
    {

    }
}

Open in new window


Now this is an extremely bare example; you'll need to fill in the details of initializing each class. The example is meant to demonstrate a possible solution to your issue, not to be a complete solution.

Author

Commented:
I like this approach, but its beyond my understanding.  What do I need to search for?

Ok, so I pass the text from column 4 to an interface IPickAName Create

This returns my class, but how do I initialize each class?  Do I have to pass the var from my LINQ as well to initialize the class?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
Here is one class, so you can see.  I'm confused on how to initialize the class with all of this data.


using LINQ2TextFile;
using System;

namespace UnityImport
{      
    
    /// <summary>
    /// This class stores data returned from the database.
    /// </summary>
    /// 
    [Serializable]
    public class CornSilage : IPickAProduct
    {
        private string scandate;
        private string equation;
        private string batchsample;
        private string serialnumber;
        private Decimal hygro;
        private Decimal hygro_nd;
        private Decimal cp;
        private Decimal cp_nd;
        private Decimal adfpro;
        private Decimal adfpro_nd;
        private Decimal ndfpro;
        private Decimal ndfpro_nd;
        private Decimal solpro;
        private Decimal solpro_nd;
        private Decimal ammonia;
        private Decimal ammonia_nd;
        private Decimal adf;
        private Decimal adf_nd;
        private Decimal ndf;
        private Decimal ndf_nd;
        private Decimal ndfs;
        private Decimal ndfs_nd;
        private Decimal lignin;
        private Decimal lignin_nd;
        private Decimal starch;
        private Decimal starch_nd;
        private Decimal sugar;
        private Decimal sugar_nd;
        private Decimal fat;
        private Decimal fat_nd;
        private Decimal ash;
        private Decimal ash_nd;
        private Decimal ca;
        private Decimal ca_nd;
        private Decimal p;
        private Decimal p_nd;
        private Decimal mg;
        private Decimal mg_nd;
        private Decimal k;
        private Decimal k_nd;
        private Decimal na;
        private Decimal na_nd;
        private Decimal s;
        private Decimal s_nd;
        private Decimal cl;
        private Decimal cl_nd;
        private Decimal rumen_buf;
        private Decimal rumen_buf_nd;
        private Decimal lactic;
        private Decimal lactic_nd;
        private Decimal acetic;
        private Decimal acetic_nd;
        private Decimal proprionic;
        private Decimal proprionic_nd;
        private Decimal isobutyric;
        private Decimal isobutyric_nd;
        private Decimal ph;
        private Decimal ph_nd;
        private Decimal tvfa;
        private Decimal tvfa_nd;
        private Decimal ndf24;
        private Decimal ndf24_nd;
        private Decimal ndf30;
        private Decimal ndf30_nd;
        private Decimal ndf48;
        private Decimal ndf48_nd;
        private Decimal asdm;
        private Decimal asdm_nd;
        private Decimal fe;
        private Decimal fe_nd;
        private Decimal ndf12;
        private Decimal ndf12_nd;
        private Decimal ivsd7;
        private Decimal ivsd7_nd;
        private Decimal indf_dm;
        private Decimal indf_dm_nd;
        private Decimal totalfa;
        private Decimal totalfa_nd;
        private Decimal solfiber;
        private Decimal solfiber_nd;
       
        /// <summary>
        /// Get the data returned from the scanDate column.
        /// </summary>
        /// 
        [Column(Name = "ScanDate", DataType = typeof(String))]
        public string ScanDate
        {
            get
            {
                return this.scandate;
            }
            set
            {
                this.scandate = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the equation column.
        /// </summary>
        [Column(Name = "Equation", DataType = typeof(String))]       
        public string Equation
        {
            get
            {
                return this.equation;
            }
            set
            {
                this.equation = value;
            }
        }        
        
        /// <summary>
        /// Get the data returned from the batchSample column.
        /// </summary>
        [Column(Name = "BatchSample", DataType = typeof(String))]
        public string BatchSample
        {
            get
            {
                return this.batchsample;
            }
            set
            {
                this.batchsample = value;
            }
        }        
        
        /// <summary>
        /// Get the data returned from the serialNumber column.
        /// </summary>
        [Column(Name = "SerialNumber", DataType = typeof(String))]
        public string SerialNumber
        {
            get
            {
                return this.serialnumber;
            }
            set
            {
                this.serialnumber = value;
            }
        }       
        
        /// <summary>
        /// Get the data returned from the HY column.
        /// </summary>
        [Column(Name = "Hygro", DataType = typeof(Decimal))]
        public Decimal HY
        {
            get
            {
                return this.hygro;
            }
            set
            {
                this.hygro = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the HY_ND column.
        /// </summary>
        [Column(Name = "HY_ND", DataType = typeof(Decimal))]
        public Decimal HY_ND
        {
            get
            {
                return this.hygro_nd;
            }
            set
            {
                this.hygro_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the CP column.
        /// </summary>
        [Column(Name = "CP",  DataType = typeof(Decimal))]
        public Decimal CP
        {
            get
            {
                return this.cp;
            }
            set
            {
                this.cp = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the CP_ND column.
        /// </summary>
        [Column(Name = "CP_ND",  DataType = typeof(Decimal))]
        public Decimal CP_ND
        {
            get
            {
                return this.cp_nd;
            }
            set
            {
                this.cp_nd = value;
            }
        }

        /// <summary>
        /// Get the data returned from the ADFPRO column.
        /// </summary>
        [Column(Name = "ADFPRO",  DataType = typeof(Decimal))]
        public Decimal ADFPRO
        {
            get
            {
                return this.adfpro;
            }
            set
            {
                this.adfpro = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the ADFPRO_ND column.
        /// </summary>
        [Column(Name = "ADFPRO_ND",  DataType = typeof(Decimal))]
        public Decimal ADFPRO_ND
        {
            get
            {
                return this.adfpro_nd;
            }
            set
            {
                this.adfpro_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NDFPRO column.
        /// </summary>
        [Column(Name = "NDFPRO",  DataType = typeof(Decimal))]
        public Decimal NDFPRO
        {
            get
            {
                return this.ndfpro;
            }
            set
            {
                this.ndfpro = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the NDFPRO_ND column.
        /// </summary>
        [Column(Name = "NDFPRO_ND",  DataType = typeof(Decimal))]
        public Decimal NDFPRO_ND
        {
            get
            {
                return this.ndfpro_nd;
            }
            set
            {
                this.ndfpro_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the SOLPRO column.
        /// </summary>
        [Column(Name = "SOLPRO",  DataType = typeof(Decimal))]
        public Decimal SOLPRO
        {
            get
            {
                return this.solpro;
            }
            set
            {
                this.solpro = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the SOLPRO_ND column.
        /// </summary>
        [Column(Name = "SOLPRO_ND",  DataType = typeof(Decimal))]
        public Decimal SOLPRO_ND
        {
            get
            {
                return this.solpro_nd;
            }
            set
            {
                this.solpro_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the AMMONIA column.
        /// </summary>
        [Column(Name = "AMMONIA",  DataType = typeof(Decimal))]
        public Decimal AMMONIA
        {
            get
            {
                return this.ammonia;
            }
            set
            {
                this.ammonia = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the AMMONIA_ND column.
        /// </summary>
        [Column(Name = "AMMONIA_ND",  DataType = typeof(Decimal))]
        public Decimal AMMONIA_ND
        {
            get
            {
                return this.ammonia_nd;
            }
            set
            {
                this.ammonia_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the ADF column.
        /// </summary>
        [Column(Name = "ADF",  DataType = typeof(Decimal))]
        public Decimal ADF
        {
            get
            {
                return this.adf;
            }
            set
            {
                this.adf = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the ADF_ND column.
        /// </summary>
        [Column(Name = "ADF_ND",  DataType = typeof(Decimal))]
        public Decimal ADF_ND
        {
            get
            {
                return this.adf_nd;
            }
            set
            {
                this.adf_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the NDF column.
        /// </summary>
        [Column(Name = "NDF",  DataType = typeof(Decimal))]
        public Decimal NDF
        {
            get
            {
                return this.ndf;
            }
            set
            {
                this.ndf = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NDF_ND column.
        /// </summary>         
        [Column(Name = "NDF_ND",  DataType = typeof(Decimal))]
        public Decimal NDF_ND
        {
            get
            {
                return this.ndf_nd;
            }
            set
            {
                this.ndf_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NDFS column.
        /// </summary>
        [Column(Name = "NDFS",  DataType = typeof(Decimal))]
        public Decimal NDFS
        {
            get
            {
                return this.ndfs;
            }
            set
            {
                this.ndfs = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the NDFS_ND column.
        /// </summary>
        [Column(Name = "NDFS_ND",  DataType = typeof(Decimal))]
        public Decimal NDFS_ND
        {
            get
            {
                return this.ndfs_nd;
            }
            set
            {
                this.ndfs_nd = value;
            }
        }

        /// <summary>
        /// Get the data returned from the LIGNIN column.
        /// </summary>
        [Column(Name = "LIGNIN",  DataType = typeof(Decimal))]
        public Decimal LIGNIN
        {
            get
            {
                return this.lignin;
            }
            set
            {
                this.lignin = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the LIGNIN_ND column.
        /// </summary>
        [Column(Name = "LIGNIN_ND",  DataType = typeof(Decimal))]
        public Decimal LIGNIN_ND
        {
            get
            {
                return this.lignin_nd;
            }
            set
            {
                this.lignin_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the STARCH column.
        /// </summary>
        [Column(Name = "STARCH",  DataType = typeof(Decimal))]
        public Decimal STARCH
        {
            get
            {
                return this.starch;
            }
            set
            {
                this.starch = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the STARCH_ND column.
        /// </summary>
        [Column(Name = "STARCH_ND",  DataType = typeof(Decimal))]
        public Decimal STARCH_ND
        {
            get
            {
                return this.starch_nd;
            }
            set
            {
                this.starch_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the SUGAR column.
        /// </summary>
        [Column(Name = "SUGAR",  DataType = typeof(Decimal))]
        public Decimal SUGAR
        {
            get
            {
                return this.sugar;
            }
            set
            {
                this.sugar = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the SUGAR_ND column.
        /// </summary>
        [Column(Name = "SUGAR_ND",  DataType = typeof(Decimal))]
        public Decimal SUGAR_ND
        {
            get
            {
                return this.sugar_nd;
            }
            set
            {
                this.sugar_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the FAT column.
        /// </summary>
        [Column(Name = "FAT",  DataType = typeof(Decimal))]
        public Decimal FAT
        {
            get
            {
                return this.fat;
            }
            set
            {
                this.fat = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the FAT_ND column.
        /// </summary>
        [Column(Name = "FAT_ND",  DataType = typeof(Decimal))]
        public Decimal FAT_ND
        {
            get
            {
                return this.fat_nd;
            }
            set
            {
                this.fat_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the ASH column.
        /// </summary>
        [Column(Name = "ASH",  DataType = typeof(Decimal))]
        public Decimal ASH
        {
            get
            {
                return this.ash;
            }
            set
            {
                this.ash = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the ASH_ND column.
        /// </summary>
        [Column(Name = "ASH_ND",  DataType = typeof(Decimal))]
        public Decimal ASH_ND
        {
            get
            {
                return this.ash_nd;
            }
            set
            {
                this.ash_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the CA column.
        /// </summary>
        [Column(Name = "CA",  DataType = typeof(Decimal))]
        public Decimal CA
        {
            get
            {
                return this.ca;
            }
            set
            {
                this.ca = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the CA_ND column.
        /// </summary>
        [Column(Name = "CA_ND",  DataType = typeof(Decimal))]
        public Decimal CA_ND
        {
            get
            {
                return this.ca_nd;
            }
            set
            {
                this.ca_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the P column.
        /// </summary>
        [Column(Name = "P",  DataType = typeof(Decimal))]
        public Decimal P
        {
            get
            {
                return this.p;
            }
            set
            {
                this.p = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the P_ND column.
        /// </summary>
        [Column(Name = "P_ND",  DataType = typeof(Decimal))]
        public Decimal P_ND
        {
            get
            {
                return this.p_nd;
            }
            set
            {
                this.p_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the MG column.
        /// </summary>
        [Column(Name = "Equation",  DataType = typeof(Decimal))]
        public Decimal MG
        {
            get
            {
                return this.mg;
            }
            set
            {
                this.mg = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the MG_ND column.
        /// </summary>
        [Column(Name = "MG_ND",  DataType = typeof(Decimal))]
        public Decimal MG_ND
        {
            get
            {
                return this.mg_nd;
            }
            set
            {
                this.mg_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the K column.
        /// </summary>
        [Column(Name = "K",  DataType = typeof(Decimal))]
        public Decimal K
        {
            get
            {
                return this.k;
            }
            set
            {
                this.k = value;
            }
        }
        /// <summary>
        /// Get the data returned from the K_ND column.
        /// </summary>
        [Column(Name = "K_ND",  DataType = typeof(Decimal))]
        public Decimal K_ND
        {
            get
            {
                return this.k_nd;
            }
            set
            {
                this.k_nd = value;
            }
        }

        /// <summary>
        /// Get the data returned from the NA column.
        /// </summary>
        [Column(Name = "NA",  DataType = typeof(Decimal))]
        public Decimal NA
        {
            get
            {
                return this.na;
            }
            set
            {
                this.na = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NA_ND column.
        /// </summary>
        [Column(Name = "NA_ND",  DataType = typeof(Decimal))]
        public Decimal NA_ND
        {
            get
            {
                return this.na_nd;
            }
            set
            {
                this.na_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the S column.
        /// </summary>
        [Column(Name = "S",  DataType = typeof(Decimal))]
        public Decimal S
        {
            get
            {
                return this.s;
            }
            set
            {
                this.s = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the S_ND column.
        /// </summary>
        [Column(Name = "S_ND",  DataType = typeof(Decimal))]
        public Decimal S_ND
        {
            get
            {
                return this.s_nd;
            }
            set
            {
                this.s_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the CL column.
        /// </summary>
        [Column(Name = "CL",  DataType = typeof(Decimal))]
        public Decimal CL
        {
            get
            {
                return this.cl;
            }
            set
            {
                this.cl = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the CL_ND column.
        /// </summary>
        [Column(Name = "CL_ND",  DataType = typeof(Decimal))]
        public Decimal CL_ND
        {
            get
            {
                return this.cl_nd;
            }
            set
            {
                this.cl_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the RUMEN_BUF column.
        /// </summary>
        [Column(Name = "RUMEN_BUF",  DataType = typeof(Decimal))]
        public Decimal RUMEN_BUF
        {
            get
            {
                return this.rumen_buf;
            }
            set
            {
                this.rumen_buf = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the RUMEN_BUF_ND column.
        /// </summary>
        [Column(Name = "RUMEN_BUF_ND",  DataType = typeof(Decimal))]
        public Decimal RUMEN_BUF_ND
        {
            get
            {
                return this.rumen_buf_nd;
            }
            set
            {
                this.rumen_buf_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the LA column.
        /// </summary>
        [Column(Name = "LA",  DataType = typeof(Decimal))]
        public Decimal LA
        {
            get
            {
                return this.lactic;
            }
            set
            {
                this.lactic = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the LA_ND column.
        /// </summary>
        [Column(Name = "LA_ND",  DataType = typeof(Decimal))]
        public Decimal LA_ND
        {
            get
            {
                return this.lactic_nd;
            }
            set
            {
                this.lactic_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the AA column.
        /// </summary>
        [Column(Name = "AA",  DataType = typeof(Decimal))]
        public Decimal AA
        {
            get
            {
                return this.acetic;
            }
            set
            {
                this.acetic = value;
            }
        }

        /// <summary>
        /// Get the data returned from the AA_ND column.
        /// </summary>
        [Column(Name = "AA_ND",  DataType = typeof(Decimal))]
        public Decimal AA_ND
        {
            get
            {
                return this.acetic_nd;
            }
            set
            {
                this.acetic_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the PA column.
        /// </summary>
        [Column(Name = "PA",  DataType = typeof(Decimal))]
        public Decimal PA
        {
            get
            {
                return this.proprionic;
            }
            set
            {
                this.proprionic = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the PA_ND column.
        /// </summary>
       [Column(Name = "PA_ND",  DataType = typeof(Decimal))]
        public Decimal PA_ND
        {
            get
            {
                return this.proprionic_nd;
            }
            set
            {
                this.proprionic_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the IA column.
        /// </summary>
        [Column(Name = "IA",  DataType = typeof(Decimal))]
        public Decimal IA
        {
            get
            {
                return this.isobutyric;
            }
            set
            {
                this.isobutyric = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the IA_ND column.
        /// </summary>
        [Column(Name = "IA_ND",  DataType = typeof(Decimal))]
        public Decimal IA_ND
        {
            get
            {
                return this.isobutyric_nd;
            }
            set
            {
                this.isobutyric_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the PH column.
        /// </summary>
        [Column(Name = "PH",  DataType = typeof(Decimal))]
        public Decimal PH
        {
            get
            {
                return this.ph;
            }
            set
            {
                this.ph = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the PH_ND column.
        /// </summary>
        [Column(Name = "PH_ND",  DataType = typeof(Decimal))]
        public Decimal PH_ND
        {
            get
            {
                return this.ph_nd;
            }
            set
            {
                this.ph_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the TVFA column.
        /// </summary>
        [Column(Name = "TVFA",  DataType = typeof(Decimal))]
        public Decimal TVFA
        {
            get
            {
                return this.tvfa;
            }
            set
            {
                this.tvfa = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the TVFA_ND column.
        /// </summary>
        [Column(Name = "TVFA_ND",  DataType = typeof(Decimal))]
        public Decimal TVFA_ND
        {
            get
            {
                return this.tvfa_nd;
            }
            set
            {
                this.tvfa_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the NDF24 column.
        /// </summary>
        [Column(Name = "NDF24",  DataType = typeof(Decimal))]
        public Decimal NDF24
        {
            get
            {
                return this.ndf24;
            }
            set
            {
                this.ndf24 = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NDF24_ND column.
        /// </summary>
        [Column(Name = "NDF24_ND",  DataType = typeof(Decimal))]
        public Decimal NDF24_ND
        {
            get
            {
                return this.ndf24_nd;
            }
            set
            {
                this.ndf24_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the NDF30 column.
        /// </summary>
        [Column(Name = "NDF30",  DataType = typeof(Decimal))]
        public Decimal NDF30
        {
            get
            {
                return this.ndf30;
            }
            set
            {
                this.ndf30 = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the NDF30_ND column.
        /// </summary>
        [Column(Name = "NDF30_ND",  DataType = typeof(Decimal))]
        public Decimal NDF30_ND
        {
            get
            {
                return this.ndf30_nd;
            }
            set
            {
                this.ndf30_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the NDF48 column.
        /// </summary>
        [Column(Name = "NDF48",  DataType = typeof(Decimal))]
        public Decimal NDF48
        {
            get
            {
                return this.ndf48;
            }
            set
            {
                this.ndf48 = value;
            }
        }
               
        
        /// <summary>
        /// Get the data returned from the NDF48_ND column.
        /// </summary>
        [Column(Name = "NDF48_ND",  DataType = typeof(Decimal))]
        public Decimal NDF48_ND
        {
            get
            {
                return this.ndf48_nd;
            }
            set
            {
                this.ndf48_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the ASDM column.
        /// </summary>
        [Column(Name = "ASDM",  DataType = typeof(Decimal))]
        public Decimal ASDM
        {
            get
            {
                return this.asdm;
            }
            set
            {
                this.asdm = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the ASDM_ND column.
        /// </summary>
        [Column(Name = "ASDM_ND",  DataType = typeof(Decimal))]
        public Decimal ASDM_ND
        {
            get
            {
                return this.asdm_nd;
            }
            set
            {
                this.asdm_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the FE column.
        /// </summary>
        [Column(Name = "FE",  DataType = typeof(Decimal))]
        public Decimal FE
        {
            get
            {
                return this.fe;
            }
            set
            {
                this.fe = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the FE_ND column.
        /// </summary>
        [Column(Name = "FE_ND",  DataType = typeof(Decimal))]
        public Decimal FE_ND
        {
            get
            {
                return this.fe_nd;
            }
            set
            {
                this.fe_nd = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the NDF12 column.
        /// </summary>
        [Column(Name = "NDF12",  DataType = typeof(Decimal))]
        public Decimal NDF12
        {
            get
            {
                return this.ndf12;
            }
            set
            {
                this.ndf12 = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the NDF12_ND column.
        /// </summary>
        [Column(Name = "NDF12_ND",  DataType = typeof(Decimal))]
        public Decimal NDF12_ND
        {
            get
            {
                return this.ndf12_nd;
            }
            set
            {
                this.ndf12_nd = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the IVSD7 column.
        /// </summary>
        [Column(Name = "IVSD7",  DataType = typeof(Decimal))]
        public Decimal IVSD7
        {
            get
            {
                return this.ivsd7;
            }
            set
            {
                this.ivsd7 = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the IVSD7_ND column.
        /// </summary>
        [Column(Name = "IVSD7_ND",  DataType = typeof(Decimal))]
        public Decimal IVSD7_ND
        {
            get
            {
                return this.ivsd7_nd;
            }
            set
            {
                this.ivsd7_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the INDF column.
        /// </summary>
        [Column(Name = "INDF",  DataType = typeof(Decimal))]
        public Decimal INDF
        {
            get
            {
                return this.indf_dm;
            }
            set
            {
                this.indf_dm = value;
            }
        }
        
        
        /// <summary>
        /// Get the data returned from the INDF_ND column.
        /// </summary>
        [Column(Name = "INDF_ND",  DataType = typeof(Decimal))]
        public Decimal INDF_ND
        {
            get
            {
                return this.indf_dm_nd;
            }
            set
            {
                this.indf_dm_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the TOTALFA column.
        /// </summary>
        [Column(Name = "TOTALFA",  DataType = typeof(Decimal))]
        public Decimal TOTALFA
        {
            get
            {
                return this.totalfa;
            }
            set
            {
                this.totalfa = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the TOTALFA_ND column.
        /// </summary>
        [Column(Name = "TOTALFA_ND",  DataType = typeof(Decimal))]
        public Decimal TOTALFA_ND
        {
            get
            {
                return this.totalfa_nd;
            }
            set
            {
                this.totalfa_nd = value;
            }
        }
                
        /// <summary>
        /// Get the data returned from the SOLFIB column.
        /// </summary>
        [Column(Name = "SOLFIB",  DataType = typeof(Decimal))]
        public Decimal SOLFIB
        {
            get
            {
                return this.solfiber;
            }
            set
            {
                this.solfiber = value;
            }
        }
        
        /// <summary>
        /// Get the data returned from the SOLFIB_ND column.
        /// </summary>
        [Column(Name = "SOLFIB_ND",  DataType = typeof(Decimal))]
        public Decimal SOLFIB_ND
        {
            get
            {
                return this.solfiber_nd;
            }
            set
            {
                this.solfiber_nd = value;
            }
        }              
    }
}

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
That's why I said it was a "bare" example  ; )

I don't know what your other classes look like, so it's difficult to tell you how to write your initialization code. One approach you may be able to use is to pass fields via the params keyword. For example:

public static IPickAName Create(string type, params string[] fields)
{
    switch (type)
    {
        case "Corn Silage":
            return new CornSilage(fields);
        case "Second Class":
            return new SecondClass(fields);
        case "Third Class":
            return new ThirdClass(fields);
        case "Fourth Class":
            return new FourthClass(fields);
        default:
            throw new ArgumentException("Type not recognized.");
    }
}

Open in new window


where your constructors would be set up to accepts a params-decorated parameter:

public class CornSilage : IPickAName
{
    public CornSilage(params string[] fields)
    {

    }
}

Open in new window


Of course, there are problems with this approach--not receiving enough parameters, if a parameter really should be an int vs a string, etc. If you're careful, you could take this approach.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

As far as the initializing part, you would just iterate through the array and assign accordingly. At this point, however, you would need to define a convention as to which index corresponds to which field. For example:

public class CornSilage : IPickAProduct
{
...

    public CornSilage(params string[] fields)
    {
        for (int i = 0; i < fields.Length; i++)
        {
            switch (i)
            {
                case 1:
                    this.ScanDate = fields[i];
                    break;
                case 2:
                    this.Equation = fields[i];
                    break;
                // etc.
            }
        }
    }

Open in new window


The above, though, isn't that extensible--if you add or remove fields later, then you'd have to readjust your assignments. It would be better if you could work the properties available for assignment into the interface somehow. But that would depend on the similarities between the classes.

Author

Commented:
The classes are very similar  They are outputs from a machine that scans the various product types.  Some may have 36 properties, others 10, but the structure is similar with the first 4 columns all being strings and in each line of the csv.

So the first part of each class is
 private string scandate;
 private string equation;
 private string batchsample;
 private string serialnumber;

each additional property is a variation of a decimal type
 private Decimal hygro;
 private Decimal hygro_nd;

Author

Commented:
My rows should actually match up with my classes, so {0}field = first property in my class

Author

Commented:
In your example, how do you pass the param[]?  So, unless I do this in the for each loop, am I passing the entire collection to nameyourfactory?  Or do i need to iterate through each row.

static void Main(string[] args)
        {
            TextFileReader csvReader = new TextFileReader(thisFile, ",");
            var thisQuery = from it2 in csvReader
                            select NameYourFactory.Create(it2[4]);
        }

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
See the code in the above snippet ( http:#codeSnippet20-36333122-1 ). It makes assumptions, though. I assumed that the "params" part of the function would essentially be "it2" (i.e. no index, the whole array). The first parameter to that function would be "it2[4]".

Author

Commented:
If I know that my class matches my param array, can I easily loop through my class to assign values
Otherwise, I'm back to a ton of code to do the following for each class.  And I'm not sure why the switch statement would be required here.  Aren't we setting every value?
public CornSilage(params string[] fields) 
    { 
        for (int i = 0; i < fields.Length; i++) 
        { 
            switch (i) 
            { 
                case 1: 
                    this.ScanDate = fields[i]; 
                    break; 
                case 2: 
                    this.Equation = fields[i]; 
                    break; 
                // etc. 
            } 
        } 
    }

Open in new window

Author

Commented:
Ok, I finallly realized what you are doing.  That makes more sense now.   But now I realize, do I really need to create a class?

If I could run this query 15 times and look for the product type in each it2[4] = "Corn Silage", etc

Couldn't I covert that to a Datatable and send it directly to sql for each product type?

TextFileReader csvReader = new TextFileReader(thisFile, ",");
                            var thisQuery = from it2 in csvReader
where it2[4].Equals("Corn Silage")
                                            select pickProduct.Create(it2[4], it2);

                            foreach (var x2 in thisQuery)
                            {
                            }
                        }

Author

Commented:
THanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial