我在Blog上工作,在我的表中,所有帖子的列标签都用逗号分隔,在post方法中,我正在发送字符串列表,我想返回所有带有我发送的列表中带有标签的文章。
我的问题是下一个,例如,文章标签为“ eBay,Shopify,亚马逊”,当我发送列表字符串“ eBay,Shopify,亚马逊”的属性时,它的效果很好,并返回我想要的结果,但是如果我发送“ Shopify,eBay,Amazon”,它不会返回任何内容,因为单词的顺序不同。
public ActionResult LoadResources(List<string> tags)
{
using (var ctx = new db_myDB())
{
if (tags != null)
{
var tagstring = "";
var lasttag = tags.Last();
foreach (var tag in tags)
{
if (!tag.Equals(lasttag))
{
tagstring += tag + ",";
}
else
{
tagstring += tag;
}
}
var blogs = ctx.Post.AsNoTracking().Where(y => y.Draft == 0 && y.Tags.Contains(tagstring)).OrderByDescending(x => x.PostedOn).Select(
post => new BlogPost
{
Published = post.Published,
Id = post.Id,
CategoryId = post.CategoryId,
ShortDescription = post.ShortDescription,
Photo = post.Photo,
UrlSlug = post.UrlSlug,
PostedOn = post.PostedOn,
Title = post.Title,
Placement = post.Placement,
Draft = post.Draft
}).ToList();
return Json(new { data = blogs }, JsonRequestBehavior.AllowGet);
}
}
}