【问题标题】:join two lists and check for the matched property value and return the required data加入两个列表并检查匹配的属性值并返回所需的数据
【发布时间】:2019-05-07 06:07:21
【问题描述】:

我有两个列表,我想比较使用 fn_cds(List1) 和 fnctn_cd (List2) 的两个列表,并从 List2 表中获取“fn_desc”。其中 List1 的“fn_cds”属于字符串 [],即表中一行中的数据将类似于 ex {fn1,fn2,fn3}。 并且数据'fnctn_cd'将出现在表中是字符串类型,即第一行中的fn1,第二行中的fn2等等。我想将这两个列表与 List1 的 fn_cds 和 List2 的 'fnctn_cd' 进行比较,并得到匹配的 'fnctn_cd' 的 'fn_desc'。

public class List1
{
  public string[] fn_cds { get; set; }
  public string fn_desc { get; set; }
  public List<List2> List2{ get; set; }
}
public class List2
{
  public string fnctn_cd { get; set; }
  public string fn_desc { get; set; }
}
public ActionResult Index()
{
  List<List1> list1= new List<List1>();
  List<List2> list2= new List<List2>();

   IEnumerable<List1> model = userGroupRepository.GetAllUserGroup().Select(o => new List1
                {
                    fn_cds = o.fn_cds,
                    fn_desc = o.fn_desc
                });
                list1 = model.ToList();

   IEnumerable<List2> model1 = userGroupRepository.GetAllFunctions().Select(o => new List2
                {
                    fnctn_cd = o.fnctn_cd,
                    fn_desc = o.fn_desc
                });
                list2 = model1.ToList();
}

我已尝试遍历如下两个列表,但没有得到预期的结果。

for(var i = 0; i < list1.Count; i++)
            {
                if(list1[i].fn_cds.Contains(list2.Any(o => o.fnctn_cd)))
                {
                    list1[i].fn_desc = list2[i].fn_desc;
                }
            }

示例表结构

Table1(list1)          Table2(list2)
----------------       -------------------------
fn_cds                  functn_cd  | fn_desc
---------------        -------------------------
{fn1,fn2,fn3}           fn1        | function1
{fn1,fn2}               fn2        | function2
{fn3}                   fn3        | function3

我的预期结果应该根据 fn_cds[](from list1) 与 fnctn_cd(from list2) 的匹配从 list2 返回 list1 中的“fn_desc”。

【问题讨论】:

  • 我会使用lcfnccmpLst。知道我在说什么吗?正确命名您的字段/方法是明智的。我不知道fn_cds 是什么。一个名为 List2 的类,它不是。 1年后你还知道吗? PascalCase/camelCase。我建议从这里开始C# Coding Conventions 它会让你的生活更轻松。也供其他程序员阅读您的代码。当然没有冒犯,只是一个建议。
  • 解释和命名有点混乱,你为什么不能使用正确的命名让别人很容易理解。
  • 您基本上是想用表2中的ID查找表1中的ID并返回描述?在这种情况下,在每个 list1 中都有一个 list2 的副本似乎有点错误,除非 list1 的每个实例都有不同的 list2 表示(是的,正如其他人所说,这个问题可以用更好/更容易的方式重新表述方式)。
  • @J.vanLangen - 是的,我知道属性名称应该以 PascalCase 开头。但这里的事情是我只是使用了 db 表中存在的相同名称。而且 List1 和 List2 不是实际名称,我只是为了快速理解而给出了这个名称。
  • 查看 table2 的样本集,作为字典而不是列表可能更有意义。

标签: c# linq


【解决方案1】:

试试这个(为简单起见,我已将您的 List&lt;List1&gt; 更改为 List&lt;string&gt;

        List<string> model = new List<string> { {"fn1,fn2"}, {"fn1"} };
        List<List2> lookup = new List<List2> 
                         {
                          new List2 {fn_desc = "Fist", fnctn_cd = "fn1"}, 
                          new List2 {fnctn_cd = "fn2", fn_desc = "Second"} 
                         };

     var result = model.Select(x => x.Split(',')
        .Select(x1 => lookup.Where(l => l.fnctn_cd == x1).Select(l => l.fn_desc).FirstOrDefault())
                    .ToList()).ToList();

虽然这看起来很丑陋,但感觉就像是 hack,但我认为如果您将 List2 更改为 Dictionary 并重新构建您的类,您可能会有更好的构造。

【讨论】:

  • 抱歉迟到了,非常感谢,当我删除“Select(x => x.Split(','))”并在“lookup.Where( l => l.fnctn_cd == x1.ToString())" 仅适用于我的相同课程。因为在我的List1类中,“fn_cds”的属性是string[],所以不需要拆分。
猜你喜欢
  • 2021-02-04
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2016-01-24
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多