我有以下LINQ查询。
var providers = from c in Repository.Query<Company>()
where !c.IsDeleted
select new { c.Description, Id = "C" + c.Id };
我试图将ID连接到&#34; C&#34;。因此,例如,如果c.Id
为35,则结果应为&#34; C35&#34;。
这显然不起作用,因为您无法在字符串中添加整数(c.Id
)。我可以使用string.Format()
或转换类型在C#中轻松解决此问题。但是我怎么能在LINQ中做到这一点?
答案 0 :(得分:2)
当你只需要准备结果时需要.NET的功能(而不是过滤,这应该在RDBMS端完成以避免在内存中引入太多数据),常见的诀窍就是在内存中完成转换使用AsEnumerable
方法:
var providers = Repository.Query<Company>()
.Where(c => !c.IsDeleted)
.Select(c => new { c.Description, c.Id }) // <<== Prepare raw data
.AsEnumerable() // <<== From this point it's LINQ to Object
.Select(c => new { c.Description, Id = "C"+c.Id }); // <<== Construct end result
答案 1 :(得分:2)
尝试使用SqlFunctions.StringConvert Method
:
var xd = (from c in Repository.Query<Company>()
where !c.IsDeleted
select new { c.Description, Id = "C" + SqlFunctions.StringConvert((double)c.Id).Trim()});
答案 2 :(得分:1)
您编写的代码可以正常工作。这是相同代码的模拟,它输出Id
的
class Company
{
public string Description { get; set; }
public int Id { get; set; }
public bool IsDeleted { get; set; }
}
static void Main()
{
//setup
var list = new List<Company>();
list.Add(new Company
{
Description = "Test",
Id = 35,
IsDeleted = false
});
list.Add(new Company
{
Description = "Test",
Id = 52,
IsDeleted = false
});
list.Add(new Company
{
Description = "Test",
Id = 75,
IsDeleted = true
});
/* code you are looking for */
var providers = from c in list
where !c.IsDeleted
select new { c.Description, Id = "C" + c.Id };
foreach (var provider in providers)
{
Console.WriteLine(provider.Id);
}
Console.ReadKey();
}
答案 3 :(得分:0)
字符串格式怎么样
var gLinks = JSON.stringify(data.items);
gLinks = gLinks.replace(/Source/g,'source');
gLinks = gLinks.replace(/Target/g,'target');
gLinks = gLinks.replace(/Value/g,'value');
var arr = JSON.parse(gLinks);