Why this Linq doesn't work (Error translating

2019-07-27 01:58发布

问题:

var query = from ch in Client.wcf.context.CashHeading
    where ch.Id_customer == customern//cc.Id
    from cs in Client.wcf.context.Cash
    where cs.Id_cashheading == ch.Id
    from gg in Client.wcf.context.Good
    where gg.Id == cs.Id_good
    select gg.Price.Value;

I'm getting inner error processing it:

Error translating Linq expression to URI: Can only specify query options (orderby, where, take, skip) after last navigation.

I can't understand why, full sources Here, on GitHub

回答1:

Basically, you have to condense your where clause into a single where clause after all of the navigations (from) have been performed, like so:

var query = 
    from ch in Client.wcf.context.CashHeading
    from cs in Client.wcf.context.Cash
    from gg in Client.wcf.context.Good
    where 
        ch.Id_customer == customern && //cc.Id
        cs.Id_cashheading == ch.Id &&
        gg.Id == cs.Id_good
    select gg.Price.Value;

Granted, this doesn't seem optimal, as it would seem that it's going to do cross join all of the tables and then perform the filtering, but remember, you're probably dealing with IQueryable<T> interface implementations, meaning that this will more than likely be interpreted and then optimized by whatever handles the translated queries.



标签: c# linq odata