How to request associations in Linq in the Entity framework in .NET Ria Services

I just started with Linq and Linq for Entity Framewok. Also using .NET Ria services. My problem is that I have 2 Folder and Item tables with many-to-many relationships using the third FolderItem connectivity table, for example: (source: InsomniacGeek.com )

Model2.jpg

In the .NET RIA Service domain service, I want to create a method that returns all the elements for a given FolderID.

In T-SQL, it would be something like this:

SELECT * FROM Item i
INNER JOIN FolderItem fi ON fi.ItemID = i.ID
WHERE fi.FolderID = 123

My Linq knowledge is limited, but I want to do something like this:

public IQueryable<Item> GetItems(int folderID)
{
  return this.Context.Items.Where(it => it.FolderItem.ID == folderID);
}

This is not the correct syntax, it throws this error:

Cannot convert lambda expression to type 'string' because it is not a delegate

( )?

- .Include("FolderItem")?

, .

PS. :

  public IQueryable<Item> GetItemsByFolderID(int folderID)
  {
    return from it in this.Context.Items
           from fi in it.FolderItem
           where fi.Folder.ID == folderID
           select it;
  }

: ?

+3
2

GetItems . :

public IQueryable<Item> GetItems(int folderID)
{
    return this.Context.FolderItems
                       .Where(fi => fi.ID == folderID)
                       .Select(fi => fi.Items);
}

.

+4

, . 2 :

1) , :

return from x in Context.FolderItems
                        .Include("FolderItem")
                        .Include("FolderItem.Item")
       where x.ID == folderID
       select x

2) , RIA , :

[MetadataTypeAttribute(typeof(FolderMetadata))]
public partial class Folder
{
     internal sealed class FolderMetadata
     {
           ...
           [Include]
           public FolderItem FolderItem; 
     }
}
+2

Source: https://habr.com/ru/post/1716729/


All Articles