3

I have this code:

   MyObject Obj {get;set;}
    var x = from xml in xmlDocument.Descendants("Master")
        select new MyObject
        {
            PropOne = //code to get data from xml
            PropTwo = //code to get data from xml
        }

The result is var being IEnumerable<MyObject>, and I need to use a foreach loop to assign x to Obj:

foreach(var i in x)
{
    Obj = i;
}

Is there a way I can use a LINQ statement and not have an IEnumerable returned in that way? Or is it normal to use a foreach loop the way I am?


Closing Modal Popup by Clicking Away from It

I am using this tutorial to add a modal screen:

http://raventools.com/blog/create-a-modal-dialog-using-css-and-javascript/

Everything works great except closing it. Rather than closing it via a button, I want to give the user the option to close it by clicking outside of the modal, i.e. in the background of the rest of the page behind it.

A user told me to add onclick='overlay()' to the overlay div like this <div id="overlay" onclick='overlay()'>

When I try to close the modal by clicking outside if it, it works, but it also closes if you click on the actual modal itself, which I don't want as it is a registration form. So is there any way to only close the modal by clicking outside of the actual modal itself?

4

3 に答える 3

6

Yep:

Obj = (from xml in xmlDocument.Descendants("Master")
        select new MyObject
        {
            PropOne = //code to get data from xml
            PropTwo = //code to get data from xml
        }).FirstOrDefault();
于 2012-05-03T21:48:22.997 に答える
1

最初のアイテムを取得する必要があります。私はあなたのLINQクエリの後にこれを個人的に作成します:

var element = xmlDocument.Descendants("Master").Single(); // Or .First() if appropriate
Obj = new MyObject
    {
        PropOne = //code to get data from xml in "element"
        PropTwo = //code to get data from xml in "element"
    };

個人的には、単一の要素を取得してから単一のオブジェクトを構築しているため、これはあなたの意図とより明確に一致していると思います。

于 2012-05-03T21:53:00.730 に答える
1

オブジェクトを 1 つだけ返す場合は、次のようにします。

 MyObject Obj {get;set;}

 var x = (from xml in xmlDocument.Descendants("Master")
    select new MyObject
    {
        PropOne = //code to get data from xml
        PropTwo = //code to get data from xml
    }).Single();

xオブジェクトのタイプは MyObject である必要があります

于 2012-05-03T21:50:24.527 に答える