如何将数据从一行复制到另一行linq?

时间:2014-01-05 02:02:26

标签: c# linq windows-phone

我有一个[表格] Book,其中一些[列]为AuthorAuthorCVEditorEditorCV

我有一些价值:

1 / Author =“ABC”,AuthorCV =“”,Editor =“哈哈”,EditorCV =“帅气”

2 / Author =“ABC”,AuthorCV =“漂亮”,Editor =“Teo”,EditorCV =“”

3 / Author =“Tom”,AuthorCV =“可爱”,Editor =“哈哈”,EditorCV =“”

我想要的是将AuthorCV从非空值复制到具有相同Author的空值 与EditorCVEditor

相同

结果是:

1 / Author =“ABC”,AuthorCV =“漂亮”,Editor =“哈哈”,EditorCV =“帅气”

2 / Author =“ABC”,AuthorCV =“漂亮”,Editor =“Teo”,EditorCV =“”

3 / Author =“Tom”,AuthorCV =“可爱”,Editor =“哈哈”,EditorCV =“英俊”

我有像这样的Viewmodel

 public class    MainViewModel : INotifyPropertyChanged
{
    private BookDataContext BookDB;
    public static string ConnectionString = "Data source=isostore:/Book.sdf";


    //****MainViewModel*****
    public MainViewModel()
    {
        BookDB = new BookDataContext(ConnectionString);

        if (!BookDB.DatabaseExists())
        {
            BookDB.CreateDatabase();
            BookDB.SubmitChanges();
       }
}

尝试答案后:

我在下面给出了一个函数....当我运行模拟器时...它有错误{System.NotSupportedException: Method 'Boolean IsNullOrEmpty(System.String)' has no supported translation to SQL.

public void copy()
{
BookDB.books.Where(b => String.IsNullOrEmpty(b.AuthorCV) || String.IsNullOrEmpty(b.EditorCV)).ToList()
            .ForEach(b =>
            {
                if (String.IsNullOrEmpty(b.AuthorCV))
                b.AuthorCV = books.FirstOrDefault(x => x.Author == b.Author && !String.IsNullOrEmpty(x.AuthorCV)).AuthorCV;
                if (String.IsNullOrEmpty(b.EditorCV))
                b.EditorCV = books.FirstOrDefault(x => x.Editor == b.Editor && !String.IsNullOrEmpty(x.EditorCV)).EditorCV;
            });
}

1 个答案:

答案 0 :(得分:0)

books.Where(b => String.IsNullOrEmpty(b.AuthorCV) || String.IsNullOrEmpty(b.EditorCV)).ToList()
                .ForEach(b =>
                {
                    if (String.IsNullOrEmpty(b.AuthorCV))
                    b.AuthorCV = books.FirstOrDefault(x => x.Author == b.Author && !String.IsNullOrEmpty(x.AuthorCV)).AuthorCV;
                    if (String.IsNullOrEmpty(b.EditorCV))
                    b.EditorCV = books.FirstOrDefault(x => x.Editor == b.Editor && !String.IsNullOrEmpty(x.EditorCV)).EditorCV;
                });