简体   繁体   English

如果不需要所有属性,是否可以将数据从一个对象放到另一个对象?

[英]Can I put the data from one object to another if I don't need all of the properties?

I have a list of PhraseAndScore 我有一个PhraseAndScore列表

public class PhraseAndScore
{
    public string PhraseId { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
    public string Kana { get; set; }
    public string Kanji { get; set; }
    public int Modified { get; set; }
    public bool Favorite { get; set; }
    public string WordType { get; set; }
    public bool Hidden { get; set; }
    public int OneHash { get; set; }
    public int TwoHash { get; set; }
    public int? FrequencyA { get; set; }
    public int? FrequencyB { get; set; }
    public int? FrequencyC { get; set; }
    public int? JapaneseForBusyPeople { get; set; }
    public int? AJlpt { get; set; }
    public int? TJlpt { get; set; }
    public bool Selected { get; set; }
    public int EnglishCorrect { get; set; }
    public int KanaCorrect { get; set; }
    public int RomajiCorrect { get; set; }
    public int KanjiCorrect { get; set; }
}

I'd like to use this data to populate a list of Phrase: 我想使用此数据填充短语列表:

public class Phrase
{
    public string PhraseId { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
    public string Kana { get; set; }
    public string Kanji { get; set; }
    public int Modified { get; set; }
    public bool Favorite { get; set; }
    public string WordType { get; set; }
    public bool Hidden { get; set; }
    public int OneHash { get; set; }
    public int TwoHash { get; set; }
    public int? FrequencyA { get; set; }
    public int? FrequencyB { get; set; }
    public int? FrequencyC { get; set; }
    public int? JapaneseForBusyPeople { get; set; }
    public int? AJlpt { get; set; }
    public int? TJlpt { get; set; }
    public bool Selected { get; set; }
}

When I try this it gives me an error: 当我尝试这样做会给我一个错误:

List<Phrase> all = db2.Query<PhraseAndScore>(select + filter);

Is there a way I can take the PhraseAndScore data and fit the columns into Phrase or would I need to do a LINQ select and itemize every column? 有没有一种方法可以获取PhraseAndScore数据并使列适合Phrase,还是需要执行LINQ选择并逐项列出每列?

I tried this also but it does not work: 我也尝试了这个,但是不起作用:

     List<Phrase> xx = all.Select(pas => new Phrase()
            {
                PhraseId = pas.PhraseId,
                CategoryId = pas.CategoryId,
                English = pas.English,
                Romaji = pas.Romaji,
                Kana = pas.Kana,
                Kanji = pas.Kanji,
                Modified = pas.Modified,
                Favorite = pas.Favorite,
                WordType = pas.WordType,
                Hidden = pas.Hidden,
                OneHash = pas.OneHash,
                TwoHash = pas.TwoHash,
                FrequencyA = pas.FrequencyA,
                FrequencyB = pas.FrequencyB,
                FrequencyC = pas.FrequencyC,
                JapaneseForBusyPeople = pas.JapaneseForBusyPeople,
                AJlpt = pas.AJlpt,
                TJlpt = pas.TJlpt,
                Selected = pas.Selected
            });

One possibility is to complete the select statement and initialize new Phrase out PhraseAndScore . 一种可能性是完成select语句,并从PhraseAndScore初始化新的Phrase

List<Phrase> all = db2.Query<PhraseAndScore>().Where(filter)
    .Select(pas => new Phrase 
        { 
            PhraseId = pas.PhraseId,
            CategoryId = pas.CategoryId
            .
            .
            .
        })

Another (more advanced) method would be to create an interface for your phrase and implement it by any object that is actually a phrase. 另一种(更高级的)方法是为您的短语创建一个接口,并通过实际上是短语的任何对象来实现它。 Then, instead of a List<Phrase> you utilize a List<IPhrase> ... 然后,使用List<IPhrase>代替List<Phrase> List<IPhrase> ...

public interface IPhrase
{
    string PhraseId { get; set; }
    int CategoryId { get; set; }
    string English { get; set; }
    string Romaji { get; set; }
    string Kana { get; set; }
    string Kanji { get; set; }
    int Modified { get; set; }
    bool Favorite { get; set; }
    string WordType { get; set; }
    bool Hidden { get; set; }
    int OneHash { get; set; }
    int TwoHash { get; set; }
    int? FrequencyA { get; set; }
    int? FrequencyB { get; set; }
    int? FrequencyC { get; set; }
    int? JapaneseForBusyPeople { get; set; }
    int? AJlpt { get; set; }
    int? TJlpt { get; set; }
    bool Selected { get; set; }
}

Your PhraseAndScore class implements it: 您的PhraseAndScore类实现了它:

public class PhraseAndScore : IPhrase
{
    public string PhraseId { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
    public string Kana { get; set; }
    public string Kanji { get; set; }
    public int Modified { get; set; }
    public bool Favorite { get; set; }
    public string WordType { get; set; }
    public bool Hidden { get; set; }
    public int OneHash { get; set; }
    public int TwoHash { get; set; }
    public int? FrequencyA { get; set; }
    public int? FrequencyB { get; set; }
    public int? FrequencyC { get; set; }
    public int? JapaneseForBusyPeople { get; set; }
    public int? AJlpt { get; set; }
    public int? TJlpt { get; set; }
    public bool Selected { get; set; }
    public int EnglishCorrect { get; set; }
    public int KanaCorrect { get; set; }
    public int RomajiCorrect { get; set; }
    public int KanjiCorrect { get; set; }
}

Your Phrase class implements it the interface too: 您的Phrase类也通过接口实现了它:

public class Phrase : IPhrase
{
    public string PhraseId { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
    public string Kana { get; set; }
    public string Kanji { get; set; }
    public int Modified { get; set; }
    public bool Favorite { get; set; }
    public string WordType { get; set; }
    public bool Hidden { get; set; }
    public int OneHash { get; set; }
    public int TwoHash { get; set; }
    public int? FrequencyA { get; set; }
    public int? FrequencyB { get; set; }
    public int? FrequencyC { get; set; }
    public int? JapaneseForBusyPeople { get; set; }
    public int? AJlpt { get; set; }
    public int? TJlpt { get; set; }
    public bool Selected { get; set; }
}

Then instantiate the list: 然后实例化列表:

List<IPhrase> all = db2.Query<PhraseAndScore>().Where(filter).ToList();

EDIT 编辑

Oh, my bad. 哦,我的坏。 List<T> requires the T argument to be invariantly valid. List<T>要求T参数始终有效。 You need a set that allows for covariant T . 您需要一个允许协变T的集合。 So you need to use IEnumerable<out T> . 因此,您需要使用IEnumerable<out T> More info on covariance. 有关协方差的更多信息。

IEnumerable<IPhrase> all = db2.Query<PhraseAndScore>().Where(filter).ToList(); // or ToArray() or whatever implementation of IEnumerable<out IPhrase>

Or, inside a method just use implicit typing: 或者,在方法内部只需使用隐式输入:

var all = db2.Query<PhraseAndScore>().Where(filter).ToList(); // or ToArray() or whatever implementation of IEnumerable<out IPhrase>

Without reflection or automapper, no, there is not. 没有反射或自动映射器,没有,没有。

Consider designing your domain model better, eg using composition: 考虑更好地设计域模型,例如使用组合:

public class Phrase
{
    public string PhraseId { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
    public string Kana { get; set; }
    public string Kanji { get; set; }
    public int Modified { get; set; }
    public bool Favorite { get; set; }
    public string WordType { get; set; }
    public bool Hidden { get; set; }
    public int OneHash { get; set; }
    public int TwoHash { get; set; }
    public int? FrequencyA { get; set; }
    public int? FrequencyB { get; set; }
    public int? FrequencyC { get; set; }
    public int? JapaneseForBusyPeople { get; set; }
    public int? AJlpt { get; set; }
    public int? TJlpt { get; set; }
    public bool Selected { get; set; }
}

public class Score
{
    public int EnglishCorrect { get; set; }
    public int KanaCorrect { get; set; }
    public int RomajiCorrect { get; set; }
    public int KanjiCorrect { get; set; }
}

public class ScoredPhrase
{
    public Phrase Phrase { get; set; }
    public Score Score { get; set; }
}

Did you try Automapper ? 您尝试过Automapper吗? It is useful to convert and load similar objects to each other. 相互转换和加载相似的对象很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum Unity:如何将一个 object 放在另一个之上 - Unity: How can I put one object on top of another 如何在插入一些数据时从数据库中获取 ID,但我没有插入稍后需要的 ID - How can I get ID from database while inserting some data, but I don't insert that ID that I need later 如何使用来自查询的另一个对象的值从linq查询中设置所有项目的属性? - How can I set properties on all items from a linq query with values from another object that is also pulled from a query? 我需要将数据从2列合并到另一列。 然后将添加的列的所有行合并到另一张表的一个单元格中 - I need to merge data from 2 columns to another column. And then merge all rows of that added column into one cell in another table 为什么我无法从其他文件访问表单属性? - Why I can't reach form properties from another file? 将数据从一个对象复制到具有有限属性的另一个对象 - Copy data from one object to another object with limited properties 我需要删除所有内容。 与模式不符 - I need to remove all . that don't match a pattern 如何从一个文件复制数据到另一个文件 - How I can copy data from one file in another one 如何将所有数据从一个单元格复制到另一个单元格? - How do I copy all data from one cell to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM