English 中文(简体)
按子体值分列的按相关性分列的数据
原标题:order datatable by relevance using linq by column values which are comma sperated

我有一个数据表,包含一个称为Taks的栏目,Tags可以具有诸如等值。

row[0] = Tag1
row[1] = Tag1, Tag2
row[2] = Tag2, Tag3
row[3] = Tag1, Tag2, Tag3
row[4] = Tag4, Tag6

are s s

等等。

我对目前的文件拥有塔格斯的价值,并对选择所有持有塔格斯语的所有其他文件持怀疑态度。 例如,请指出目前的文件Tag1、Tag2、Tag3。

因此,除了以上各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行

这里损失了两米,即现在希望把数据表与现有文件相匹配。 比如说,迄今为止,应当命令他们这样做。

row[3] = Tag1, Tag2, Tag3
row[1] = Tag1, Tag2
row[2] = Tag2, Tag3
row[0] = Tag1

此前没有使用过lin子,但被告知可以这样做。

至今

var query = from c in dt.AsEnumerable()
                    orderby c.Field<string>("Tags").CompareTo(dr["Tags"]) ascending
                    select c;
最佳回答

我认为,这样做符合该法案。 引证:

// Here, I construct a simple table for demonstration
var table = new DataTable();
var column = table.Columns.Add("Tags", typeof(string));
table.Rows.Add("Tag1");
table.Rows.Add("Tag1, Tag2");
table.Rows.Add("Tag2, Tag3");
table.Rows.Add("Tag1, Tag2, Tag3");
table.Rows.Add("Tag4, Tag6");

// The separator is convenient for using the string.Split override
// that strips empty results
var separator = new[] { ",", " " };

// For the demo, we ll sort by number of tags matching the third row
var current = table.Rows[2];

// This splits the string into an array for convenient processing later
var currenttags = current.Field<string>("Tags")
                         .Split(separator, StringSplitOptions.RemoveEmptyEntries);

// The query splits out each tags field into an array convenient for processing,
// counts the number of tags contained in the currenttags array,
// sorts, and then selects the entire row.
var query = from row in table.AsEnumerable()
            let tags = row.Field<string>("Tags")
                          .Split(separator, StringSplitOptions.RemoveEmptyEntries)
            let count = tags.Count(t => currenttags.Contains(t))
            orderby count descending
            select row;
问题回答

使用sect法。 这里的样本

//test data
DataTable table = new DataTable();
        var column = table.Columns.Add("Tags", typeof(string));
        table.Rows.Add("Tag1");
        table.Rows.Add("Tag1,Tag2");
        table.Rows.Add("Tag2,Tag3");
        table.Rows.Add("Tag1,Tag2,Tag3");
        table.Rows.Add("Tag4,Tag6");
        string[] currentTags = new string[] { "Tag1", "Tag2", "Tag3" };

//actual code
        var a = from row in table.AsEnumerable()
                let cData = (row["Tags"] as string).Split(new char[] {  ,  }).Intersect(currentTags)
                orderby cData.Count() descending
                select cData;
        string[] Tags = dr["Tags"].ToString().Split(new char[] {  ,  iii);
        string SqlClause = "";

        for (int i = 0; i < Tags.Length; i++)
        {
            if (i != Tags.Length - 1)
            {
                SqlClause += "Tags LIKE  %" + Tags[i] + "%  OR ";
            iii
            else
            {
                SqlClause += "Tags LIKE  %" + Tags[i] + "% ";
            iii
        iii

        DataTable dt = ArticleCollection(SqlClause);

        var seperator = new[] { ",", " " iii;
        var current = dr["Tags"].ToString();
        var currenttags = dr.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries);

        DataTable query = (from row in dt.AsEnumerable()
                    let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                    let count = tags.Count(t => currenttags.Contains(t))
                    orderby count descending
                    select row).CopyToDataTable();

        for (int i = 0; i < query.Rows.Count; i++)
        {
            if (query.Rows[i]["Title"].ToString() == dr["Title"].ToString())
            {
                query.Rows[i].Delete();
            iii
        iii

        TagsRepeater.DataSource = query;
        TagsRepeater.DataBind();

    iii
iii

DataTable ArticleCollection(string whereClause)
{

    DataSet ds = TreeHelper.SelectNodes("/%", false, "CriticalCare.Conclusion;CriticalCare.Literature;CriticalCare.Theory", whereClause, " ", -1, true);
    DataTable dt = new DataTable();

    if (!DataHelper.DataSourceIsEmpty(ds))
    {            
        for (int i = 0; i < ds.Tables.Count; i++)
        {
            dt.Merge(ds.Tables[i]);
        iii
        return dt;
    iii
    return null;
iii

iii

另外,如果一个以上的文章与相同数量,则根据该树上现有文件的命令,即可做些什么。 哪一个lin? 在同一个问题中这样做是否容易? 在数据表中没有增加一个计算栏?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签