1

我有一些如下查询

var email= (from c in dataContext.tblC
            where (c.AA == aa)
            select c.email).ToList();
string emails = email.Aggregate((a, b) => a + "," + b);

现在我需要另一列 SecEmail,我不能只是 (...select c.email, c.SecEmail).ToList()。任何关于我的建议都可以得到列表(email1、SecEmail1、email2、email3、SecEmail3、email4...)

4

1 回答 1

0

如果您正在使用动态对象:

var email = (from c in dataContext.tblC
             where c.AA == aa
             select new {
               email = x.email,
               secemail = c.secEmail,
               // ...
             }).ToList(); // IList<dynamic> & IDE will know what properties
                          // you supplied in the `select new { }`

否则构建一个模型并填充它:

public class SelectModel
{
    public String email { get; set; }
    public String secemail { get; set; }
}

var email = (from c in dataContext.tblC
             where c.AA == aa
             select new SelectModel {
               email = x.email,
               secemail = c.secEmail,
               // ...
             }).ToList(); // IList<SelectModel>

如果您希望将返回的行转换为电子邮件to标题:

var email = String.Join(", ", (
              from c in dataContext.tblC
              where c.AA == aa
              select c.email
            ).AsEnumerable());

这将使:

+------------------+
| email            |
|------------------|
| foo@contoso.com  |
| bar@contoso.com  |
| baz@contoso.com  |
+------------------+

变成:

foo@contoso.com, bar@contoso.com, baz@contoso.com

多列连接:

var email = (from c in dataContext.tblC
             where c.AA == AA
             select new { email = c.email, secEmail = c.secEmail }).AsEnumerable();
var to = String.Join(", ",
    email.Select(x => x.email)
        .Concat(email.Select(y => y.secEmail))
        // .Concat(eail.Select(y => x.thirdColumn))
        // ...
);
于 2013-08-29T15:43:21.840 回答