Why does LinkEntity.AddLink initialize the LinkFromEntityName with it's own, LinkFromEntityName, rather than it's LinkToEntityName?

邮差的信 提交于 2019-12-11 03:37:38

问题


I'm trying to generate this basic SQL statement using Query Expressions:

SELECT *
FROM contact
INNER JOIN businessunit on contact.businessunitid = businessunit.businessunitid
INNER JOIN new_example on businessunit.new_exampleid = new_example.new_exampleid

Using this Query Expression Test:

var query = new QueryExpression("contact");
var bu = query.AddLink("businessunit", "businessunitid", "businessunitid");
var buChildLink = bu.AddLink("new_example", "new_exampleid", "new_exampleid");

Assert.AreEqual("businessunit", buChildLink.LinkFromEntityName); // Fails. Actual value is "contact"

The fix is to not use the AddLink method, but create the LinkEntity where you specify the LinkFromEntityName, but am I wrong in thinking this is a bug?


回答1:


I've created an overloaded generic AddChildLink method that correctly handles this issue so that the link gets added correctly within the query expression. I've also added some overloads that default some of the parameters.

/// <summary>
/// Created do to possible bug (http://stackoverflow.com/questions/10722307/why-does-linkentity-addlink-initialize-the-linkfromentityname-with-its-own-lin)
/// Adds the new LinkEntity as a child to this LinkEnity, rather than this LinkEntity's LinkFrom Entity
/// Assumes that the linkFromAttributeName and the linkToAttributeName are the same
/// </summary>
/// <param name="link"></param>
/// <param name="linkToEntityName"></param>
/// <param name="linkAttributesName"></param>
/// <returns></returns>
public static LinkEntity AddChildLink(this LinkEntity link, string linkToEntityName, string linkAttributesName)
{
    return link.AddChildLink(linkToEntityName, linkAttributesName, linkAttributesName);
}

/// <summary>
/// Created do to possible bug (http://stackoverflow.com/questions/10722307/why-does-linkentity-addlink-initialize-the-linkfromentityname-with-its-own-lin)
/// Adds the new LinkEntity as a child to this LinkEnity, rather than this LinkEntity's LinkFrom Entity
/// Assumes that the linkFromAttributeName and the linkToAttributeName are the same
/// </summary>
/// <param name="link"></param>
/// <param name="linkToEntityName"></param>
/// <param name="linkAttributesName"></param>
/// <param name="joinType"></param>
/// <returns></returns>
public static LinkEntity AddChildLink(this LinkEntity link, string linkToEntityName, string linkAttributesName, JoinOperator joinType)
{
    return link.AddChildLink(linkToEntityName, linkAttributesName, linkAttributesName, joinType);
}

/// <summary>
/// Created do to possible bug (http://stackoverflow.com/questions/10722307/why-does-linkentity-addlink-initialize-the-linkfromentityname-with-its-own-lin)
/// Adds the new LinkEntity as a child to this LinkEnity, rather than this LinkEntity's LinkFrom Entity
/// </summary>
/// <param name="link"></param>
/// <param name="linkToEntityName"></param>
/// <param name="linkFromAttributeName"></param>
/// <param name="linkToAttributeName"></param>
/// <returns></returns>
public static LinkEntity AddChildLink(this LinkEntity link, string linkToEntityName,
    string linkFromAttributeName, string linkToAttributeName)
{
    return link.AddChildLink(linkToEntityName, linkFromAttributeName, linkToAttributeName, JoinOperator.Inner);
}

/// <summary>
/// Created do to possible bug (http://stackoverflow.com/questions/10722307/why-does-linkentity-addlink-initialize-the-linkfromentityname-with-its-own-lin)
/// Adds the new LinkEntity as a child to this LinkEnity, rather than this LinkEntity's LinkFrom Entity
/// </summary>
/// <param name="link"></param>
/// <param name="linkToEntityName"></param>
/// <param name="linkFromAttributeName"></param>
/// <param name="linkToAttributeName"></param>
/// <returns></returns>
public static LinkEntity AddChildLink(this LinkEntity link, string linkToEntityName,
    string linkFromAttributeName, string linkToAttributeName, JoinOperator joinType)
{
    var child = new LinkEntity(
        link.LinkToEntityName, linkToEntityName,
        linkFromAttributeName, linkToAttributeName, joinType);
    link.LinkEntities.Add(child);
    return child;
}

public static LinkEntity AddLink(this QueryExpression qe, string linkToEntityName, string linkAttributesName)
{
    return qe.AddLink(linkToEntityName, linkAttributesName, linkAttributesName);
}

public static LinkEntity AddLink(this QueryExpression qe, string linkToEntityName, string linkAttributesName, JoinOperator joinType)
{
    return qe.AddLink(linkToEntityName, linkAttributesName, linkAttributesName, joinType);
}

This shortens method and allows for chaining:

var qe = new QueryExpression("new_entitya");
qe.AddLink("new_entityb", "new_entitybid").
    AddChildLink("new_entityc", "new_entitybid").
    LinkCriteria.AddCondition("new_entitycid", ConditionOperator.Equal, id);


来源:https://stackoverflow.com/questions/10722307/why-does-linkentity-addlink-initialize-the-linkfromentityname-with-its-own-lin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!