我第一次使用LINQ并且有以下查询:
var query = from c in context.Call
join d in context.CallData on c.CallID = d.CallID
where c.CallID == 123
select new {
c.CallID,
c.CallResult,
d.FieldID,
d.FieldValue
};
返回类似于以下内容的数据
c.CallID c.CallResult d.FieldID d.FieldValue
123 Sale 345 John
123 Sale 346 Doe
123 Sale 347 888-222-3333
如何向下钻取到包含d.FieldID = 346的行以获取d.FieldValue的值(346是LastName字段)?我想将此值用作局部变量的新值:
// I know this doesn't work, just showing what I would like to accomplish
string LastName = query.Select(a => a.FieldID = 346).FieldValue;
相反,一旦得到这些结果,我怎么能改变c.CallResult的值?既然它是一个连接,我可以简单地更改一行数据,还是必须进行单独的查询?
答案 0 :(得分:0)
你的意思是:
string LastName = query
.Where(a => a.FieldID = 346)
.Select(a => a.FieldValue)
.FirstOrDefault();
答案 1 :(得分:0)
对于第一个问题,您可以从query
变量继续:
var fieldValue = query.Where(x => x.FieldID == 346).Single().FieldValue;
关于第二个问题:您必须对原始实体对象应用更改:
var call = context.Find(123);
// or context.Calls.Single(c => c.CallId == 123) if this is not DbContext.
call.CallResult = newValue;
context.SaveChanges();