I have a list of two properties named Kanjis
:
and I would like to use that to update the contents of another list named PhraseSources
public class PhraseSource
{
[PrimaryKey]
public string Id { get; set; }
public int PhraseNum { get; set; }
public string English { get; set; }
public string Kanji { get; set; }
public string WordType { get; set; }
public string FrequencyA { get; set; }
}
by matching together Kanji > Text and updating FrequencyA with Code
Is this something that can be done with LINQ or is there a better to way to do this by iterating through each of the rows of the phraseSource, checking for a matching entry in Kanjis and doing an update that way?
Here's the code suggested by Salva that I tried:
(from sa in source
join d in psDb on sa.Text equals d.Kanji
let temp = d.FrequencyA = sa.Code
select 0).ToList();
gives error:
ApplyFrequency.cs(14,14): Error CS1941: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'. (CS1941) (Download)
source.Join(psDb, s => s.Text, d => d.Kanji, (s, d) => d.FrequencyA = s.Code).ToList();
gives error:
ApplyFrequency.cs(21,21): Error CS0411: The type arguments for method 'Enumerable.Join(IEnumerable, IEnumerable, Func, Func, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. (CS0411) (Download)
Notes:
I had to use this code as I was asked to change FrequencyA to an int:
var source = original
.Select(x => new
{
Text = x.Text,
Code = Convert.ToInt32(x.Code.Substring(2))
})
.ToList();
You can update list through LINQ only. The closest you could get is:
static void Main(string[] args)
{
List<SomeClass> scl = new List<SomeClass>();
List<OtherClass> ocl = new List<OtherClass>();
foreach (var item in scl)
item.FrequencyA = ocl.Where(i => i.Text == item.Kanji).FirstOrDefault()?.Code ?? null;
}
// sample classes that reflect relevant properties
public class SomeClass
{
public string FrequencyA { get; set; }
public string Kanji { get; set; }
}
public class OtherClass
{
public string Code { get; set; }
public string Text { get; set; }
}
You can do it via single linq query syntax :
var source = new List<TextCodeClass>();
var dest = new List<PhraseSource>();
(from s in source
join d in dest on s.Text equals d.Kanji
let temp = d.FrequencyA = s.Code.ToString()
select 0).ToList();
or via method syntax :
source.Join(dest, s => s.Text, d => d.Kanji, (s, d) => d.FrequencyA = s.Code.ToString())
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.