Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:01:31+00:00 2026-06-06T16:01:31+00:00

i have a number of Tables setup in SQl Server i have transferred all

  • 0

i have a number of Tables setup in SQl Server
i have transferred all of these into a dbml file for normal LinqToSql Use.

i’m wondering if it is possible to alter the class in code to only select the Columns of the table and not the Linked tables

i.e. if i wanted to pass the class to a method without the linked table properties how would i go about doing it

more information essentially i’m trying to use an extension i have written to use SQLbulkCopy instead of context.SubmitChanges but run into mapping problems due to extra properties

I realise I could use anonymous types but thought that would overwrite any benefit of the Extension methods.
the other option is to alter the ToDataTable extension.

     public static void SqlBulkInsert<T>(this IEnumerable<T> source, string connectionString, string tableName)
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            var bulkCopy = new SqlBulkCopy(conn) {DestinationTableName = tableName};
            conn.Open();
            var item = source.ToDataTable();
            bulkCopy.WriteToServer(item);
            conn.Close();
        }
    }
    public static DataTable ToDataTable<T>(this IEnumerable<T> source)
    {
        using (var dt = new DataTable())
        {
            var toList = source.ToList();

            for (var index = 0; index < typeof(T).GetProperties().Length; index++)
            {
                var info = typeof(T).GetProperties()[index];
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }

            for (var index = 0; index < toList.Count; index++)
            {
                var t = toList[index];
                var row = dt.NewRow();
                foreach (var info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }

            return dt;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:01:33+00:00Added an answer on June 6, 2026 at 4:01 pm

    Right so i solved it

    Usage Example

    _list = new List<Item>();
    //Fill _list
    _list.SqlBulkInsert(SettingsFile.ConnectionString,"Table");
    

    i have had to define a static List and only process those property types contained within the list

    a collegue suggested using the isclass attribute on propertyType but this had issues
    of course
    i would be happy for some critique if there are any other methods or something i may have missed

    hope that this helps someone!

    i.e. like this

          private static List<Type> Types
        {
            get
            {
                return new List<Type>
                       {
                           typeof (String),
                           typeof (int?),
                           typeof (Guid?),
                           typeof (double?),
                           typeof (decimal?),
                           typeof (float?),
                           typeof (Single?),
                           typeof (bool?),
                           typeof (DateTime?),
                           typeof (int),
                           typeof (Guid),
                           typeof (double),
                           typeof (decimal),
                           typeof (float),
                           typeof (Single),
                           typeof (bool),
                           typeof (DateTime),
                           typeof (DBNull)
                       };
            }
        }
    
     public static DataTable ToDataTable<T>(this IEnumerable<T> source)
        {
            using (var dt = new DataTable())
            {
                var toList = source.ToList();
    
                for (var index = 0; index < typeof(T).GetProperties().Count(); index++)
                {
                    var info = typeof(T).GetProperties()[index];
                    if (Types.Contains(info.PropertyType))
                    {
                        dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
                    }
                }
    
                for (var index = 0; index < toList.Count; index++)
                {
                    var t = toList[index];
                    var row = dt.NewRow();
                    foreach (var info in typeof(T).GetProperties())
                    {
                        if (Types.Contains(info.PropertyType))
                        {
                            row[info.Name] = info.GetValue(t, null);
                        }
                    }
                    dt.Rows.Add(row);
                }
    
                return dt;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of LINQ to SQL tables with the same fields (
I have a number of manually written scripts (.sql) for tables, views and stored
I have a number of tables that use the Postgres Partitioning feature. I want
I have a number of tables, which have nested tables. I using jQuery to
I have a number of tables containing some basic (business related) mapping data. What's
May be a stoopid question: I have a number of reference tables in an
I have a number of static classes that contain tables like this: using System;
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have two tables: subject and student. I'm trying to count the number of
I need to transfer a large number of rows from a SQL Server database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.