-2

我有使用 linq 编写的代码

var result=from m in driver_list select(m.Email,m.LisancePlate)

我的代码输出是这样的

sample1@company.com;sample2@company.com----34 KZ 7898 sample3@company.com;sample2@company.com---- 34 TZK 6785

我想使用我的结果生成一个新的输出,例如 sample1@company.com---34 KZ 7898 sample2@company.com---34 KZ 7898 sample3@company.com---34 TZK 6785 sample2@company.com- --34 TZK 6785

4

4 回答 4

0

从给定的输出我想你有一个像这样的列表:

List<string> test = new List<string>()
{
    "sample1@company.com;sample2@company.com----34 KZ 7898",
    "sample3@company.com;sample2@company.com---- 34 TZK 6785"
};

以下将产生请求的输出:

var result = 
test
.Select 
(
    t =>new 
    {
        Emails =  t.Split(new string[] { "----" }, StringSplitOptions.None)[0],
        LicensePlate = t.Split(new string[] { "----" }, StringSplitOptions.None)[1]
    }
)
.Select 
(  
    t => new
    {
        Email = t.Emails.Split(';'),
        t.LicensePlate}
    )
.SelectMany 
( 
    t => t.Email,
    (x,t)=>new {Email = t,CarPlaket = x.LicensePlate)
)
.ToList();

foreach(var email in result)
    Console.WriteLine("{0}---{1}",email.Email,email.CarPlaket;)

上述解决方案不会将结果放在同一个列表中,但会创建一个新列表。

于 2013-10-05T08:17:46.817 回答
0

看起来你必须使用Regex,因为你有多个spacesin 34 KZ 7898。我已经尝试过这段代码,它可以按您的预期工作:

string input = "sample1@company.com;sample2@company.com----34 KZ 7898 sample3@company.com;sample2@company.com---- 34 TZK 6785";
var result = string.Join(" ", Regex.Split(input, "\\s(?=\\S+@)")
                             .Select(x=>{
                                var s = x.Split(new string[] {"----"}, StringSplitOptions.RemoveEmptyEntries);
                                return string.Join(" ",s[0].Split(';').Select(e => e + "----" + s[s.Length - 1]).ToArray());
                             }).ToArray());//No need ToArray() since .NET 4.0
//Output
sample1@company.com---34 KZ 7898 sample2@company.com---34 KZ 7898 sample3@company.com---34 TZK 6785 sample2@company.com---34 TZK 6785
于 2013-10-05T08:23:20.000 回答
0

.Net 的正则表达式引擎支持环视中的组捕获..所以它变得简单!

string input="sample1@company.com;sample2@company.com----34 KZ 7898 sample3@company.com;sample2@company.com---- 34 TZK 6785";
String[] emailValues=Regex.Matches(input,@"(\w+@\w+[.]\w+)(?=.*?(-+.*?)(?=\s*\w+@|$))")
                          .Cast<Match>()
                          .Select(x=>x.Groups[1].Value+x.Groups[2].Value)
                          .ToArray();

这将适用于任意数量的电子邮件ID

于 2013-10-05T08:31:19.620 回答
0
var src_in_arac = from log in Src_Log_List.ToList()
                              join ml in mail_arac_sorgu on log.Node equals ml.TasitTanımaNo
                              join sr in surucu_list.Select(ss =>
                              {
                                  string SCKartNo = "";
                                  string AdSoyad = ss.AdSoyad;

                                  if (ss.SrcKardId != "")
                                      SCKartNo = "000" + (Convert.ToInt64(ss.SrcKardId).ToString("X"));
                                  return new { AdSoyad, SCKartNo };

                              })
                              on log.SCID equals sr.SCKartNo
                              into data
                              from ss in data.DefaultIfEmpty()
                              where log.Task == "GPSEvents"


                              select new { log.Speed, AdSoyad = (ss == null ? "İZİNSİZ KULLANIM" : ss.AdSoyad), ml.Plaka, log.OdoMeter, log.StandStill, log.Date, ml.Grup, ml.Email ,log.SC};

我的代码在上面。我很抱歉我的英语不好。邮件字段是 ml.Email ,我想拆分加入 ';' 的邮件 章。然后进入sama字符串。

于 2013-10-06T17:22:54.047 回答