使用linq将一个表中的行保存到另一个表中

时间:2022-06-02 00:25:26

You know when you drag and drop a table from the dataSources window onto a usercontrol? Visual studio automatically creates a datacontext (in the view's xaml) to bind the new elements on the page to.This dataContext is set to a collectionViewSource and this collectionViewSource draws it's information from a dataset. The same dataset you took the table from when draging and dropping.

您知道何时将表从dataSources窗口拖放到用户控件上? Visual Studio自动创建一个datacontext(在视图的xaml中)以将页面上的新元素绑定到。此dataContext设置为collectionViewSource,此collectionViewSource从数据集中绘制它的信息。在拖放时从表中获取的相同数据集。

In my scenario this is what it created:

在我的场景中,这是它创建的:

<UserControl.Resources>
    <CollectionViewSource x:Key="signupsViewSource" Source="{Binding signups, Source={StaticResource myAppnDataSet}}/>
</UserControl.Resources>

<Grid DataContext="{StaticResource signupsViewSource}">
    <TextBox x:Name="idTextBox" Text="{Binding id, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox x:Name="firstnameTextBox" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox x:Name="surnameTextBox" Text="{Binding surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox x:Name="usernameTextBox" Text="{Binding username, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox x:Name="passwordTextBox" Text="{Binding password, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>  

It's supposed to be a very simple signup page that an administrator can use to approve and deny registrations made by simply scrolling through, clicking the approve button or clicking the reject button.

它应该是一个非常简单的注册页面,管理员可以通过简单地滚动,单击批准按钮或单击拒绝按钮来批准和拒绝注册。

The thing is I'm using the MVVM pattern. This means that the properties the textBoxes above are bound to, are in the collectionViewSource class, which is where my problem begins.

问题是我正在使用MVVM模式。这意味着上面的textBoxes绑定的属性位于collectionViewSource类中,这是我的问题开始的地方。

I need to save this same information from the 'signups' table, to the users table.

我需要将这些相同的信息从'signups'表保存到users表。

  1. Since the properties the textBoxes are bound to are in the collectionViewSource, Is there any way to fetch their values from there?
  2. 由于textBoxes绑定的属性位于collectionViewSource中,有没有办法从那里获取它们的值?
  3. I looked through the solution explorer and did not find any actual class called signupViewSource. If it doesn't it exist, then how is it referenced and why is it even called a class?
  4. 我查看了解决方案资源管理器,但没有找到任何名为signupViewSource的实际类。如果它不存在,那么它是如何引用的,为什么它甚至被称为类?
  5. Is it a place holder until I create my own collectionViewSource class? Will it be more feasible that way?
  6. 在我创建自己的collectionViewSource类之前,它是一个占位符吗?这种方式会更可行吗?

What I've tried so far is the following method that is supposed to save the information from the signups table to the users table:

我到目前为止尝试的是以下方法,该方法应该将注册表中的信息保存到users表:

private object SaveCurrent_CommandExecute(object param)
{
    myAppEntities context = new myAppEntities();

    myApp.signup Signup = new signup();

    try
    {
        myApp.user User = new Medcare2.user
        {
            firstname = Signup.first,
            surname = Signup.last,
            username = Signup.username,
            password = Signup.password,
        };

        // Persist Changes to database
        context.users.Add(User);
        context.SaveChanges();
        MessageBox.Show("Saved");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  

The problem with the above method is that it creates a new instance of the signups class I'm saving from, so all the information in the properties I'm grabbing are empty. So I get validation errors. basically. Any help would be much appreciated.

上述方法的问题在于它创建了一个我正在保存的注册类的新实例,因此我抓取的属性中的所有信息都是空的。所以我得到验证错误。基本上。任何帮助将非常感激。

1 个解决方案

#1


1  

You need to pass your selected Signup object as the CommandParameter on whatever control is invoking the SaveCurrent command. Then you can cast object param back to a Signup and extract the values from there.

您需要在调用SaveCurrent命令的任何控件上将所选的Signup对象作为CommandParameter传递。然后,您可以将对象参数转换回注册并从那里提取值。

#1


1  

You need to pass your selected Signup object as the CommandParameter on whatever control is invoking the SaveCurrent command. Then you can cast object param back to a Signup and extract the values from there.

您需要在调用SaveCurrent命令的任何控件上将所选的Signup对象作为CommandParameter传递。然后,您可以将对象参数转换回注册并从那里提取值。