简体   繁体   English

将对象传递给linq结果

[英]Passing object to linq result

I get an item from the list to manuplate it. 我从列表中得到一个项目来处理它。

App.CurrentQuestion = App.AllQuestionList[dataSourceRowIndex];

After manuplations I save new data to database and get back from database into App.CurrentQuestion, so App.CurrentQuestion's list reference breaks. 进行完操作后,我将新数据保存到数据库,然后从数据库返回到App.CurrentQuestion中,因此App.CurrentQuestion的列表引用中断了。 I want to update list and trying to focus selected item by linq 我想更新列表并尝试通过linq集中选中的项目

App.AllQuestionList
    .Where(q => q.qID == App.CurrentQuestion.qID)
    .FirstOrDefault() = App.CurrentQuestion;

but I get an error like " The left-hand side of an assignment must be a variable, property or indexer " 但出现类似“ 分配的左侧必须是变量,属性或索引器 ”的错误

I can use this method 我可以用这种方法

for (int i = 0; i < App.AllQuestionList.Count; i++)
{
    if (App.AllQuestionList[i].qID == App.CurrentQuestion.qID)
    {
        App.AllQuestionList[i] = App.CurrentQuestion;
        break;
    }
}

but looking for an alternative method. 但正在寻找另一种方法。 Or is there any faster method? 还是有更快的方法?

You shouldn't have to do anything, since it's by reference. 您无需做任何事情,因为它只是参考。

App.CurrentQuestion = App.AllQuestionList[dataSourceRowIndex];

Whatever change you make in App.CurrentQuestion should be reflected in the App.AllQuestionList 无论您对App.CurrentQuestion任何更改,都应反映在App.AllQuestionList

App.AllQuestionList[App.AllQuestionList.IndexOf(App.AllQuestionList.
    .Where(q => q.qID == App.CurrentQuestion.qID)
    .First())] = App.CurrentQuestion;

Edit: you can just use the IndexOf to find the index of the object you wanted to find by LINQ query 编辑:您可以只使用IndexOf来查找要通过LINQ查询查找的对象的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM