如何拆分存储在 list<*> 中的字符串,然后使用 linq 样式或 lambda 插入到同一列表中

本文关键字:linq 样式 然后 lambda 列表 插入 拆分 何拆分 存储 list 字符串 | 更新日期: 2023-09-27 18:34:05

I have Writen o code using 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

如何拆分存储在 list<*> 中的字符串,然后使用 linq 样式或 lambda 插入到同一列表中

从给定的输出中,我有一个列表,如下所示:

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;)

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

.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

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

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
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,我想拆分用";"连接的邮件。然后进入萨马字符串。