Вопрос

I'm trying to replicate the following SQL query in linq:

Select
     l.*,
    ISNULL( i.InterestPercentage,0)
     as InterestPercentage
FROM properties l
LEFT JOIN interest i on i.ListingKey = l.ListingKey
Where i.userId = {0}

I don't really have much to go with at the moment:

var results = from l in context.properties
              join s in context.interest on l.ListingKey equals s.ListingKey
              where s.userId == "";

This returns me a full join, but I'd like to return the properties with a single additional value, the InterestPercentage. I think I might need to create a new object that is all the columns of properties with an additional property of InterestPercentage. Then add select new MyObject { tons of property setters }.

Additionally though, I'm trying to expose this via Odata, would I lose the queryable ability by doing this?

Это было полезно?

Решение

You can try to return

new {MainObj = l, InterestPercentage = (your calculated field)}

Or create an object which will have similar structure as above. This will help you avoid all property setting.

Другие советы

When you expose this query via OData you will have to rewrite it not to use join. OData does not support joins but is supports expansions, i.e. fetching related collections in a single request. In order to be able to expand your OData query you need a respective relationship to be defined in the metadata, i.e. if you want to fetch data from both Properties and Interest using ListingKey as a join field, you will need a relationship between these tables based on ListingKey (or another table that refers to these two).

You don't have to use join, you can simulate a join by making your own explicit join:

var results = from prp in context.properties
              from inr in context.interest // cross join

              // your own join phrase
              where inr.ListingKey == (prp.InterestPercentage ?? 0)
                    && inr.userId == ""
              select new { ... };
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top