SharePoint c# caml query two joined lists - c#

Hi I have two lists in sharepoint:
1,Page- Id, title
2,Fragment- TableAId, article
One page can have more fragments.
Is it possible to join these two lists and get in C# one object with nested list of articles?
I saw its possible by caml query but I am not successful so far.
I tried code as its in the picture
I tried this caml query in the picture. And googled in stack overflow. It should be possible but I don't know how...

Related

How can I get a nested IN clause with a linq2sql query?

I am trying to implement some search functionality within our app and have a situation where a User can select multiple Topics from a list and we want to return all activities that match at least one of the selected Topics. Each Activity can have 0-to-many Topics.
I can write a straight SQL query that gives me the results I want like so:
SELECT *
FROM dbo.ACTIVITY_VERSION av
WHERE ACTIVITY_VERSION_ID IN (
SELECT ACTIVITY_VERSION_ID
FROM dbo.ACTIVITY_TOPIC at
WHERE at.TOPIC_ID IN (3,4)
)
What I can't figure out is how to write a LINQ query (we are using Linq to Sql) that returns the same results.
I've tried:
activities.Where(x => criteria.TopicIds.Intersect(x.TopicIds).Any());
this works if activities is a list of in memory objects (i.e. a Linq to Objects query), but I get an error if I try to use the same code in a query that hits the database. The error I receive is:
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
I believe that this means that Linq to Sql doesn't know how to translate either Intersect or Any (or possibly both). If that is the case, I understand why it isn't working, but I still don't know how to make it do what I want it to and my Google-fu has not provided me with anything that works.
Haven't tested it. But this is how you ll go about it.
List<int> IDs = new List<int>();
IDs.Add(3);
IDs.Add(4);
var ACTIVITY_VERSION_IDs = ACTIVITY_TOPIC
.Where(AT => IDs.Contains(AT.TOPIC_ID))
.Select(AT=>AT.ACTIVITY_VERSION_ID)
var results = ACTIVITY_VERSION
.Where(AV => ACTIVITY_VERSION_IDs.Contains(AV.ACTIVITY_VERSION_ID))

Dynamic LINQ to join on dynamic columns on DataTable

I have a situation that I am currently unsure of how to proceed.
I have two datatables that are populated from a database, I also have a list of column names available that can be used to join these two datatables together. I wish to write a set of LINQ queries that will:
Show rows from both datatables (inner join, used for updating one from another).
Show rows from one datatable that don't exist in the other (one query, left join used for inserts, the other a right join used for deletes).
Now I know how to do this with normal LINQ to objects or datatables, however in this case I need to apply the columns to join on dynamically, and there could be more than one. Looking at the following partial example code:
table1.AsEnumerable()
.Join(table2.AsEnumerable(),
dr1 => dr1.Field<string>("ID"),
dr2 => dr2.Field<string>("ID"),
(dr1, dr2) => new
{
FieldID = dr1.Field<string>("ID"),
CdGroup = dr2.Field<string>("Name")
})
The issues are that I don't know the field type so the .Field<string> parts of the statement can't be applied. Also if their are multiple join columns, then I will need to have multiple join statements.
I have read up on dynamic LINQ and it seems quite promising, however I haven't managed to find any information on dynamic LINQ joins like I am trying to do. I know I could probably get the same results using nested loops or the.Select() method on the datatable, but I am trying to apply LINQ to some of the tougher queries that I require.
Do anyone have any pointers or examples of how I could achieve this, or should I just revert to using a non-LINQ approach?
Thanks very much.
If you are using Entity Framework, you could download the Microsoft Dynamics CRM, and use the pattern answered to this question Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of conditions
There is a construct called QueryExpression from which you can model dynamic queries. See this MSDN article http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.queryexpression.aspx

Getting similar items from database

I have Sql table that contains two fields Title and Description, I use LINQ to SQL for all datbase operations, how would I get similar items depending on the item that I am currently showing to the user, I know this can be done by using Full Text Search in SQL but LINQ to SQL doesn't support it.
Can someone explain me logic beheind this, or show me example how it's done? Should I split Title and Description on words and then search the table or? SO similar questions system seems perfect, how do they do it?
There is a function called Contains() , I think It will solve your problem , here is an example about how to use It :
db = new YourDataContext();
List<Table> list = new List<Table>();
list = (from i in db.Table where (i.Item.Contains(curentItem)))select i).ToList();

One DataAdapter, two different tables

I find it really hard to explain what is going on, I'll try my best.
We need to make a really simple browser with really simple Favorites + History capabilities, and to do that we need to import them both in a DataSet and use them from there.
The knowledge that I have should be enough, but I would really like to do it more efficient en cleaner. In the database I have 3 tables, one for users, one for favorites and one for history, this is linked with FK's etc. I want a query that returns me every Fav + History url that a user has saved. This is what I have now:
SELECT u_id, u_user, h_url, f_url FROM Users, Favorites, HistoryWHERE u_id = h_id AND u_id = f_id AND u_id = 1
This isn't the result I'm looking for, I want to fill 2 comboboxes, one with favorites and one with history that both are from the person that is logged in at that moment.
I know this should work with join but inner and outer both give to little or to many results, and left and right join don't seem to work for me either, but I cant explain why. :p I'm semi-new to joins btw.
It sounds like you actually want two simple queries, one for favorites and one for history. Both queries just need to have a where clause limiting the user for whom the results are returned.
SELECT f.url
FROM Favorites f
WHERE f.userID = 1
SELECT h.url
FROM History h
WHERE h.userID = 1
If you want to populate two comboboxes, I would suggest that you retrieve two separate lists, one for each combobox. Then you can bind each list separately to its combobox.
While DataSets may not be the best choice, I think they will work for you in this situation. Your objective will be to write two SQL statements, one for each list. Then, you put the query results into DataTables which are contained in one DataSet. A DataTable can be boung to a combobox.

Union on two big LINQ queries not supported. How do I get around it (if at all possible)

This is quite a monster query I am writing.
A very quick description of the database: You have an image table, an imagetag table, which joins it to a tag table. Images can have 0 or many tags.
I have a query which does a full text search on the Image's title property and this works fine.
However, I want it so when you do a full text search, it also looks at the image's tag names to see if anything matches. For instance, you could have an image with a title of "Awesome Cakes", which has a tag of cooking. When a user does a full text search of cooking, it should find that image, because it has a corresponding tag.
Right?
So as I mentioned I have my fulltext method which works and returns a queryable list of images.
I have also made a method which finds images with matching tags to the full text query
IQueryable<Image> results = imageService.FullTextSearch(MakeSearchTerm(freeText));
IQueryable<Image> tagResults = imageService.FullTextTagSearch(freeText, tagService);
When I debug this and view the enumerations, they both have results.
What I want to do is union them into one result set.
The reason I want to do this, is later down the code, other filters are applied to the results before the actual query is executed, such as filtering by author and only taking the results needed for the particular page.
Unfortunately, when I try and union:
results = results.Concat(tagResults).Distinct();
Types in Union or Concat cannot be constructed with hierarchy.
I understand that this might not be possible :p But I'm just seeing if there are any good ideas and solutions, cheers.
I think there is a problem in the way you approach searching. Instead of returning Images, you should introduce a type that only captures the image metadata, with a key to the image.
I dont't get exactly what "Types in Union or Concat cannot be constructed with hierarchy" should mean though. Is that an exception?
I would try combining the FullTextSearch and FullTextTagSearch functions into something like this:
public IQueryable<Image> ImageSearch(string fullText)
{
return from image in ImageTable
where image.title.Contains(fullText)
|| image.tagName.Contains(fullText)
select image;
}

Categories

Resources