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 894597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:25:03+00:00 2026-05-15T14:25:03+00:00

My LINQ knowledge is ok. I need a loop or another method to accomplish

  • 0

My LINQ knowledge is ok. I need a loop or another method to accomplish ADO.NET method (see foreach loop)

public bool AccessProcess(string sp, ListDictionary ld, CommandType cmdType)
{
    SqlConnection con = new SqlConnection(
        WebConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlCommand cmd = new SqlCommand(sp, con);
    try
    {
        con.Open();
        cmd.CommandType = cmdType;
        foreach (string ky in ld.Keys)
        {
            cmd.Parameters.AddWithValue(ky, ld[ky]);
        }
        cmd.ExecuteNonQuery();
    }
    finally
    {
        con.Dispose();
        cmd.Dispose();
    }
    return true;
}

I need:

foreach (string ky in ld.Keys)
    yourObject.SetPropertyValue(ky, ld[ky]);

How can I do that? Can I use LINQ-to-SQL?

Can you give clear sample like

using (var contex = new DataclassContext())
foreach (string ky in ld.Keys)
{
    contex.Parameters.AddWithValue(ky, ld[ky]);
}
contex.SubmitChanges();
  • 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-05-15T14:25:04+00:00Added an answer on May 15, 2026 at 2:25 pm

    Linq to SQL maps data classes (often called an ‘Entities’) to a SQL database table.

    So, first you need to use the Linq->SQL designer to generate these data classes for you, and a DataContext object that will map theses classes to and from the database.

    You usually define this mapping in the Linq to SQL designer UI in Visual Studio. This UI let’s you visually edit a .dbml file and essentially defines a mapping between your CLR entity objects and the backing SQL database table.

    See this MSDN article for an example of how to add a new Linq to SQL class.

    Once you have defined this class, you can create a new instance of this class and its property values. These property values are mapped to columns in the SQL database table. You then pass this object to the DataContext class generated by LINQ to SQL, to add a new row to the table in the database.

    So, you would do something like this:

    For a database table with columns "ColumnA", "ColumnB" and "ColumnC"
    
    var myEntity = new EntityObject { ColumnA = "valueA", ColumnB = "valueB", "ColumnC" = "valueC" };
    DataContext.InsertOnSubmit(myEntity);
    DataContext.SubmitChanges();
    

    This will insert a new row into the database with the column values specified.

    Now if you don’t want to manually write code to write to ColumnA, ColumnB, etc, then you could use reflection to write to the appropriate properties on the entity:

    For example, with entity instance ‘myEntity’:

    var properties = myEntity.GetType().GetProperties();
    
    foreach (string ky in ld.Keys)
    {
        var matchingProperty = properties.Where(p => p.Name.Equals(ky)).FirstOrDefault();
        if (matchingProperty != null)
        {
            matchingProperty.SetValue(myEntity, ld[ky], null);
        }
    }
    

    Using reflection doesn’t give the best performance, so I would question why you even want to map an array of keys and values to a linq->sql entity, rather than using the entity object directly in the code that currently generates these key/value pairs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guess people need some experience with Linq-to-xml and knowledge of the build of a
LINQ is one of the greatest improvements to .NET since generics and it saves
Linq in general, has extensions methods(at IEnumerable) like Where, Select, OrderBy. But use another
Why linq is trying to check second expression anyway? .Where(t => String.IsNullOrEmpty(someNullString) || t.SomeProperty
Since I still have a limited knowledge of LINQ, I figured I would ask
I tried to use linq but failed because i have no xml knowledge. What
Below method selects admin rights and returns bool from a cached DataTable, would it
I know LINQ but my knowledge is pretty much only selects, where, orderby and
LINQ library in .NET framework does have a very useful function called GroupBy ,
i have a .net dll which is written in c sharp, which uses linq

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.