[英]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();
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.