开发者

Linq to entities to join two tables

Is there any way to 'join' two DB tables so I could get ID from 1st table then use this ID to get the description from a 2nd table?

For example, the follow query gets the Part ID but I need find the description for this Part ID so I could put the description onto a data grid. I tried to use 'Include' on Web server side but it failed during runtime.

var query = myContext.Get_Table1();
q开发者_开发知识库uery = query.Where(c => c.Part_ID == '12345');
LoadOperation<My.Web.Table1> loadOp = this.maxContextTransactionHistory.Load(query, QueryCompletedCallback, null);
this.dataGrid.ItemsSource = loadOp.Entities;


I take it that you are using WCF RIA Services? It would have been helpful if you had tagged your question to indicate this.

If this is the case, you will need to use the Include method within your DomainService to include the related entity in the results of the query.

You will also need to apply the IncludeAttribute to the association to ensure that they are marshalled to the client. You can apply the attribute to a 'Buddy' class which is covered here.

In theory it would be sufficient to apply the IcludeAttribute directly to the properties within the generated entity classes. There, they would be overwritten.

The next choice would be in a partial class. Adding another property there with the same signature (so that the attribute can be applied to it) would cause a conflict with the original property.

The solution is to use a new class that is defined within a partial class. The MetadataType attribute is used on the partial class to indicate the class in which its metadata is defined.

The following code will ensure that a Part_Stock's Part_Table is include when it is sent to the client. You will need similar code to cover any other properties that you are interested in.

[MetadataTypeAttribute(typeof(Part_Stock.Metadata))]
public partial class Part_Stock
{

    internal sealed class Metadata
    {
        // Metadata classes are not meant to be instantiated.
        private Metadata()
        {
        }

        [Include]
        public EntityCollection<Stock_Table> Stock_Table { get; set; }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜