Linq要sql选择新的字符串

时间:2013-10-04 17:09:57

标签: c# .net linq

所以我得到了这段代码:

ClassA { List<ClassB> classBList;}
ClassB { List<ClassC> classCList;}
ClassC { String ID;}

Dictionary<string,int> dict;
ClassA MyObject;

字典上的键应与ClassC上的ID字段匹配 我想在linq上做以下查询

List<String> matches = (from b in MyObject
                      from c in b.classCList
                      join d in dict
                      on c.ID equals dict.Key
                      select new
                      {
                          c.Value == 0 ? "YES" : "NO"
                      }).ToList();

但我收到错误:"Invalid anonymous type member ..."

底线是......我如何在select new中有条件?

修改

如何使用扩展方法执行此查询?

有任何帮助吗? TY

1 个答案:

答案 0 :(得分:10)

您正在尝试选择匿名类型,然后尝试将结果分配回List<string>。您的查询应该是:

List<String> matches = (from b in MyObject.classBList
                        from c in b.classCList
                        join d in dict
                        on c.ID equals d.Key
                        select d.Value == 0 ? "YES" : "NO").ToList();