-1

我有这个 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; }

}

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

什么工作但不是很好:我现在发现的唯一解决方案是将 FormQry 转换为List<forma>s1 组件,并通过遍历所有组件(其中 16k :S)将列表排除在外

4

2 回答 2

0
var result = FormQry.Select(f=>f.s1)
                    .Where(s=>s != "null")
                    .Distinct().ToList();
于 2013-11-03T12:55:56.377 回答
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);    
        }
    }
于 2013-11-03T12:02:30.810 回答