简体   繁体   English

用linq替换字符串

[英]String replacement with linq

I have this query it is suppose to get the ip addresses and match them to the DeviceUIDs which is located on another table. 我有这个查询,它假设是要获取IP地址并将它们与位于另一个表上的DeviceUID匹配。 I am looking to replace the "." 我正在寻找替换“。” in the IP Address with "0" but the way I have the code running right now it does not replace the "." 在IP地址中以“ 0”表示,但是我现在运行代码的方式不能替换“。”

Can anyone help me with this please. 谁能帮我这个忙。

 var etjoin = (from e in dxlXs.AsEnumerable()
              join t in tstarresults on new String(e.Field<String>("Ip Address").Replace(".","0").ToArray()) equals t.DeviceUID
              into leftjointable
              from x in leftjointable.DefaultIfEmpty()
              select new ATTModel
              {
                  ATTIP = e != null ? e.Field<String>("IP Address") : "N/A",
                  ATTICCID = e != null ? e.Field<String>("ICCID") : "N/A",
                  ATTSTATUS = e != null ? e.Field<String>("SIM Status") : "N/A",
                  ATTSession = e != null ? e.Field<String>("In Session") : "N/A",
                  ATTActivated = e != null ? e.Field<String>("Activated") : "N/A",
                  IP = x != null ? x.DeviceUID : "N/A",
                  VehicleName = x != null ? x.VehicleName : "N/A",
                  VehicleGroupName = x != null ? x.VehicleGroupName : "N/A",
                  PhoneNumber = x != null ? x.UserDefinedColumn2 : "N/A"
              }).ToList();

First String.Replace returns a new string with replaced characters, so you don't need the constructor new String(char[]) ( ctor ). First String.Replace返回一个带有替换字符的新字符串,因此您不需要构造函数new String(char[])ctor )。

Second there is nothing explicit wrong with the string replacement. 其次,字符串替换没有明显的错误。 eg "127.0.0.1".Replace(".","0") returns "127000001" So you have to be more precise on what is "wrong". 例如"127.0.0.1".Replace(".","0")返回"127000001"因此您必须更精确地了解“错误”。

Third your logic is a left join so if you're expecting null values for e when creating your ATTModel you have to check for null in e.Field<String>("Ip Address") too. 第三,您的逻辑是左连接,因此,如果在创建ATTModel时期望e nullATTModel也必须在e.Field<String>("Ip Address")检查null。 (one can guess dxlXs is a DataTable or a DataSet thus the rows are not likely being null ) (可以猜测dxlXs是一个DataTableDataSet,因此行不太可能为null

With your replace in the Join , you are creating a new string just for the purposes of making the join. 使用Join的替换项时,您将仅为了进行连接而创建一个新字符串。 That string is assigned or stored anywhere, so e.Field<String>("Ip Address") will still have the same value (with the dots). 该字符串被分配或存储在任何地方,因此e.Field<String>("Ip Address")仍将具有相同的值( e.Field<String>("Ip Address") )。

If you need ATTIP to have the value with the . 如果您需要ATTIP来将值包含在中. replaced with 0 (although I'm not sure that really makes sense), then you could do something like: 替换为0 (尽管我不确定这是否真的有意义),那么您可以执行以下操作:

 var etjoin = (from e in dxlXs.AsEnumerable()
              let temp = e != null ? e.Field<String>("Ip Address").Replace(".","0") : "N/A"
              join t in tstarresults on temp equals t.DeviceUID
              into leftjointable
              from x in leftjointable.DefaultIfEmpty()
              select new ATTModel
              {
                  ATTIP = temp,
                  //...
              }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM