包含IEnumerable到List <string> </string>的字符串的列

时间:2013-11-03 11:50:50

标签: c# string linq ienumerable

在搜索谷歌多次并四处寻找答案后,我决定在这里寻求帮助。

问题如下: 我有这个linq:

            var FormQry =
                (from a in db.deriv_form
                join b in db.deriv_affix on a.s1_id.Value equals b.id into t1
                from g1 in t1.DefaultIfEmpty()
                join c in db.deriv_affix on a.s2_id.Value equals c.id into t2
                from g2 in t2.DefaultIfEmpty()
                join d in db.deriv_affix on a.s3_id.Value equals d.id into t3
                from g3 in t3.DefaultIfEmpty()
                join e in db.deriv_affix on a.p1_id.Value equals e.id into t4
                from g4 in t4.DefaultIfEmpty()
                join f in db.deriv_affix on a.p2_id.Value equals f.id into t5
                from g5 in t5.DefaultIfEmpty()
                join g in db.deriv_affix on a.p3_id.Value equals g.id into t6
                from g6 in t6.DefaultIfEmpty()
                join h in db.deriv_affix on a.p4_id.Value equals h.id into t7
                from g7 in t7.DefaultIfEmpty()
                join i in db.deriv_stem on a.o1_id equals i.id into t8
                from g8 in t8.DefaultIfEmpty()
                join j in db.deriv_root on g8.root_id equals j.id into t9
                from g9 in t9.DefaultIfEmpty()
                select new forma()
                {
                    form = a.form,
                    s1 = g1.allomorph == null ? "null" : g1.allomorph,
                    s2 = g2.allomorph == null ? "null" : g2.allomorph,
                    s3 = g3.allomorph == null ? "null" : g3.allomorph,
                    p1 = g4.allomorph == null ? "null" : g4.allomorph,
                    p2 = g5.allomorph == null ? "null" : g5.allomorph,
                    p3 = g6.allomorph == null ? "null" : g6.allomorph,
                    p4 = g7.allomorph == null ? "null" : g7.allomorph,
                    root = g9.morpheme
                }).AsEnumerable<forma>();

我可爱的形式课看起来像

    public class forma
{
    public string form { get; set; }
    public string s1 { get; set; }
    public string s2 { get; set; }
    public string s3 { get; set; }
    public string p1 { get; set; }
    public string p2 { get; set; }
    public string p3 { get; set; }
    public string p4 { get; set; }
    public string root { get; set; }

}

WISH: 我想要的是从var FormQry选择s1列删除所有空值,将其区分并将其转换为List<string>

什么工作但不是很好: 我现在找到的唯一解决方案是将FormQry转换为List<forma>并从那里获取s1组件并通过迭代所有这些组件来列出它(其中16k:S)

2 个答案:

答案 0 :(得分:0)

您可以使用ConvertAll实现此类转换。请参阅下面的代码示例:

   class special
   {
        public string something = "";
   }

    class Program
    {
        static void Main(string[] args)
        {
            List<special> list = new List<special>();

            List<string> stringlist = list.ConvertAll(c => c.something);    
        }
    }

答案 1 :(得分:0)

var result = FormQry.Select(f=>f.s1)
                    .Where(s=>s != "null")
                    .Distinct().ToList();