Вопрос

I'm stuck, hoping someone out there can kick me in the right direction.

I have a ListView where I've been looping out data from a single db-table, using model binding to do this (Web forms project btw). So far so good. Now I'm in need of checking some values in another table, to modify the look of the objects beeing looped out. Essentially what i want to do is, I have 2 buttons in my ListView, if a record in the table i want to join exists, one of the buttons should get the visible = false attribute.

So I've been trying to write a LINQ-query to do this, so far without much success.

Example of a query I've written

var query2 = from l in myContext.Table1
                             join p in myContext.Table1.Where(p => p.pID == 1)
                             on l.pID equals p.pID
                             where l.UserId == 1

This only returns the records that do exists in the table i join, i want all the records in Table1 to be presented - and after that run some kind of if-statement to check if a record in the joined table exists or not, causing a button to either show or not.

Further information, I'm using a SelectMethod returning a IQueryable.

Also, since using model binding, do i need to declare a new class for this joined object, or is there a simpler way (since this will only be used once on the whole site).

Thanks in advance for any hints or help!

Update

Using a ListView to loop out my data

Default.aspx

    <asp:ListView ID="MyListView" runat="server" ItemType="Project.Models.Object" SelectMethod="MyListView_GetData">
        <ItemTemplate>
<!-- HERE i want to either add a button, or don't -->
        </ItemTemplate>
    </asp:ListView>

Default.aspx.cs

public IQueryable<Project.Models.Object> MyListView_GetData()
        {
            try
            {
                int qs2 = 2;
                var user = User.Identity.GetUserId().ToString();

                var query = from p in myContext.Object.Where(p => p.pID== qs2)
                            select p;

                foreach (var item in query)
                {
                    var doesExist = myContext.Object2.Any(m => m.eID == item.eID);

                    if (doesExist)
                    {
                        // Need to do something here?
                    }
                }

                return query.OrderByDescending(item => item.eID);
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Desc.");
                return null;
            }
        }
Это было полезно?

Решение

You can use Any() to check if a record exists. So if you wanted to check if a record exists with UserId of 99 you could do:

var doesExist = myContext.Table2.Any(m => m.UserId == 99);

That returns a bool. You can easily use that bool to set your button

button.Visible = !doesExist;

Edit for your comment:

After your done with your first query, go ahead and determine if the record exists, and then switch the button's visibility with the result.

For example if you have some method called DoWork that executes and populates your listbox:

void DoWork() {
    ... execute your first query
    ... populate your listbox

    var doesExist = myContext.Table2.Any(m => m.UserId == 99);
    yourButton.Visible = !doesExist;
}

I can't really say where exactly you should put this without seeing the rest of your code. But since you mentioned, "i want all the records in Table1 to be presented - and after that run some kind of if-statement to check if a record in the joined table exists or not, causing a button to either show or not", it's clear that it has to happen after your first query.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top