C# 如果我不需要所有属性,我可以将数据从一个对象放到另一个对象吗?
我有一份措辞和分数的清单C# 如果我不需要所有属性,我可以将数据从一个对象放到另一个对象吗?,c#,linq,C#,Linq,我有一份措辞和分数的清单 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
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);
是否有一种方法可以获取短语和分数数据并将列放入短语中,或者我需要执行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
});
一种可能是完成select语句并初始化新短语out PhraseAndScore 您的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; }
}
您的短语类也在接口中实现它:
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();
或者,在方法内部只使用隐式类型:
var all = db2.Query<PhraseAndScore>().Where(filter).ToList(); // or ToArray() or whatever implementation of IEnumerable<out IPhrase>
你试过了吗?
将相似的对象相互转换和加载很有用。没有反射或自动映射,不,没有 考虑更好地设计域模型,例如使用组合:
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; }
}
select+filter在做什么?您可以使用Automapper库,它可以为您完成这项工作。您可以使用反射枚举所有属性并复制这两个类中存在的属性。或者简单地从一个有lessI的继承人那里继承一个有更多信息的继承人我尝试了你提到的继承人,但是它仍然说它不能转换:public类短语和分数:短语{public int EnglishCorrect{get;set;}public int KanaCorrect{get;set;}public int RomajiCorrect{get;set;}public int KanjiCorrect{get;set;}}您可以在PhraseAndScore中提供一个方法public static GetPhrasePhraseAndScore pac{return new Phrase{…}}。然后您只需要实现一次属性映射,您的LIQN查询看起来像db2.QueryPhraseAndScore.getphrase可能太难了,因为我只需要这个,我喜欢接口的想法,但当我尝试它时,我仍然得到相同的消息无法转换。这可能是因为我正在使用列表吗?我也尝试过像您建议的那样使用select,但这给了我一个问题。我在question.var all=db2.Queryselect+filter中添加了我尝试的内容;列出abc=all,其中x=>x.EnglishCorrect>0;我想这不会有什么帮助,因为我在列表中选择了SQL,那么复杂类型呢?
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>
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; }
}