English 中文(简体)
LinkEnty为何要使用 LinkEnty. AddLink 初始化“ 链接EntyName ”, 而不是“ LinkTo Enty”?
原标题:Why does LinkEntity.AddLink initialize the LinkFromEntityName with it s own, LinkFromEntityName, rather than it s LinkToEntityName?

我试图用查询表达式生成此基本的 SQL 语句 :

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

使用此查询表达式测试 :

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"

修正是不要使用 AddLink 方法, 而是在您指定 LinkFromEntity Name 的地方创建链接实体, 但我认为这是错误吗?

最佳回答

我创建了超载的通用 AddChildLink 方法, 正确处理此问题, 以便在查询表达式中正确添加链接 。 我还添加了默认某些参数的超载 。

/// <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);
}

这缩短了方法,并允许链条:

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

暂无回答




相关问题
Howto get started with C# 4.0 and .NET 4.0?

I don t want to download Visual Studio 2010. How can I start studying (not developing real applications) C# 4.0 and .NET 4.0 with just a text editor? Can I just download C# 4.0 compiler and .NET 4.0 ...

Mocking Framework with C# 4.0 Support?

Anybody know of a mocking framework that supports C# 4.0? Doesn t matter which one ATM, just need something that will work.

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface. Now when I come to test the ...

How to Iterate Through Array in C# Across Multiple Calls

We have an application where we need to de-serialize some data from one stream into multiple objects. The Data array represents a number of messages of variable length packed together. There are no ...

IronPython ScriptRuntime equivalent to CPython PYTHONPATH

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program. import ConfigParser C# code: using System; using System.Collections.Generic; using ...

i cant understand the following code

Matrix<float> trainData2 = trainData.GetRows(intVar >> 1, intVar, 1); intVar is integer type... please help me to understand this code.

热门标签