我有一个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; }
}
我想用这些数据填充一个词组列表:
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; }
}
当我尝试这个时,它给了我一个错误:
List<Phrase> all = db2.Query<PhraseAndScore>(select + filter);
有没有办法可以获取PhraseAndScore数据并使列适合Phrase,还是我需要进行LINQ选择并列出每列?
我也试过了,但它不起作用:
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
});
答案 0 :(得分:2)
一种可能性是完成select
语句并初始化新Phrase
PhraseAndScore
。
List<Phrase> all = db2.Query<PhraseAndScore>().Where(filter)
.Select(pas => new Phrase
{
PhraseId = pas.PhraseId,
CategoryId = pas.CategoryId
.
.
.
})
另一种(更高级的)方法是为您的短语创建一个接口,并通过任何实际上是短语的对象来实现它。然后,您使用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; }
}
您的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; }
}
您的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; }
}
然后实例化列表:
List<IPhrase> all = db2.Query<PhraseAndScore>().Where(filter).ToList();
哦,我的坏。 List<T>
要求T
参数不变地有效。您需要一个允许协变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>
或者,在方法内部只使用隐式输入:
var all = db2.Query<PhraseAndScore>().Where(filter).ToList(); // or ToArray() or whatever implementation of IEnumerable<out IPhrase>
答案 1 :(得分:1)
没有反射或自动播放器,不,没有。
考虑更好地设计您的域模型,例如使用构图:
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; }
}
答案 2 :(得分:0)
你试过Automapper吗? 将类似的对象相互转换和加载很有用。