我做了一个FetchXML查询,从我的CRM2013网格返回数据 数据将传递到列表中 然后使用以下代码将其转换为CSV文件;
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml)); foreach (var c in result.Entities)
{
if (result != null && result.Entities.Count > 0)
{
List<string> _product = new List<string>();
foreach (Entity _entity in result.Entities)
{
_product.Add(((EntityReference)c.Attributes["productid"]).Name.ToString());
_product.Add(_entity.Attributes["quantity"].ToString());
}
CSVFile = string.Join(",", _product.ToArray());
string AddressPath = "FM-OR" + "_";
string AddressSavePath = @"\\fm\CRMdata\maesteg\" + AddressPath + ".csv";
System.IO.File.WriteAllText(AddressSavePath, CSVFile.ToString());
输出如下
ProductExample1,1.0,ProductExample2,4.0,ProductExample3,2.0
我想要的是现在的输出
ProductExample1,1.0
ProductExample2,4.0
ProductExample3,2.0
关于如何实现这一目标的任何建议?
由于
更新
我觉得我没有正确解释它 使用我想要的输出,我希望它们是单独的字符串,以便我可以为列表中的每个产品+数量输出不同的CSV文件 感谢
更新
实施新建议的代码,错误在下面的评论中描述
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml)); foreach (var c in result.Entities)
{
if (result != null && result.Entities.Count > 0)
{
List<string> _product = new List<string>();
foreach (Entity _entity in result.Entities)
{
string productid = (((EntityReference)_entity.Attributes["productid"]).Name.ToString());
string quantity = _entity.Attributes["quantity"].ToString();
CSVFile = productid + "," + quantity;
int n =1;
string AddressPath = "FM-OR" + "_" +actualID + "_" + n;
string AddressSavePath = @"\\fm\CRMdata\maesteg\" + AddressPath + ".csv";
System.IO.File.WriteAllText(AddressSavePath, CSVFile.ToString());
n++;
}
}
}
由于
答案 0 :(得分:0)
这种扩展方法怎么样?
public static IEnumerable<string> JoinEverySecond(this IList<string> list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
for (var i = 0; i < list.Count; i += 2)
{
if (i + 1 >= list.Count)
{
yield return list[i];
}
yield return string.Join(",", list[i], list[i + 1]);
}
}
警告: 未经测试,但我想提出这个想法。
答案 1 :(得分:0)
根据我对您的问题的理解,这可能会对您有所帮助。 CSVFILE
现在是一个字符串,其中包含由&#34;,&#34;分隔的productid和数量。您可以写入您的文件。
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));
if (result != null && result.Entities.Count > 0)
{
List<string> _products = new List<string>();
foreach (Entity _entity in result.Entities)
{
string productid = (EntityReference)c.Attributes["productid"]).Name.ToString();
string quantity = _entity.Attributes["quantity"].ToString();
CSVFILE = productid + "," + quantity;
//Write CSVFILE
//...
}
}