1

I have an ICollectionView built from a LINQ to Entities query comprised of an anonymous object ...I want to apply filter to this collection, however the Filter property expects a Predicate

What can I do in order to apply the filter on this anonymously typed collection?

var playerDatabase = (from p in NFLDataContext.DimPlayers
                    join c in NFLDataContext.DimColleges on p.CollegeID equals c.CollegeID
                    join pos in NFLDataContext.DimPositions on p.PositionID equals pos.PositionID
                    select new
                    {
                        FirstName = p.FirstName,
                        LastName = p.LastName,
                        FullName = p.FirstName + " " + p.LastName,
                        CollegeName = c.CollegeName,
                        CollegeID = p.CollegeID,
                        PositionCode = pos.PositionCode,
                        PositionName = pos.PositionName,
                        PositionID = p.PositionID,
                        PlayerID = p.PlayerID
                    }).ToList();


        dgPlayers.ItemsSource = playerDatabase;

Elsewhere in an event handler ....

        string SomeFilterCondition;
        ICollectionView view = CollectionViewSource.GetDefaultView(dgPlayers.ItemsSource);
        view.Filter = (item) =>
        {
             ?????????????
        };

It's hard to provide the statement you want without the full details of your schema. For example, the question refers to a news table and an artists table, but doesn't provide the schemas for those, or indicate how the statement that contains those references relate to any of the other tables mentioned in the question.

Still, I think what you want can be done entirely in MySQL, without any fun PHP tricks, especially if there are common fields in each of the various tables.

But first: this might not be the answer you're really wanting, but using triggers on your various tables to update an "events feed" table is likely the best solution. i.e., when an insert or update happens on the "status" table, have a trigger on the status table that inserts into the "events feed" table the ID of the person, and their type of action. You could have a separate insert and update trigger to indicate different events for the same data type.

Then it'd be super-easy to have an events feed, because you're just selecting straight from that events feed table.

Check out the create trigger syntax.

That said, I think you might have a look at the CASE and UNION keywords.

You can then construct a query that grabs data from all tables and outputs strings indicating something. You could then turn that query into a view, and use that as an "events feed" table to select directly from.

Say you have a list of members (which you do), and the various tables that contain actions from those members (i.e., tracks, status, pics, videos), which all have a key pointing back to your members table. You don't need to select from members to generate a list of activity, then; you can just UNION together the tables that have certain events.

SELECT
    events.member_id
  , events.table_id
  , events.table
  , events.action
  , events.when_it_happened
  , CASE
      WHEN events.table = "tracks" THEN "Did something with tracks"
      WHEN events.table = "status" THEN "Did something with status"
    END
    AS feed_description
FROM (
  SELECT
      tracks.ID AS member_id
    , tracks.track_ID AS table_id
    , "tracks" AS table
    , CONCAT(tracks.url, ' ', tracks.name) AS action
    , tracks.timestamp AS when_it_happened
  ORDER BY tracks.timestamp DESC
  LIMIT 10

  UNION

  SELECT
      status.ID as member_id
    , status.status_id AS table_id
    , "status" AS table
    , status.value AS action
    , status.timestamp AS when_it_happened
  ORDER BY status.timestamp DESC
  LIMIT 10

  UNION
  ...

) events
ORDER BY events.when_it_happened DESC

I still think you'd be better off creating a feed table built by triggers, because it'll perform a lot better if you're querying for the feed more often than generating events.

4

3 に答える 3

3

動的にキャストするのはどうですか?次のようなものです:

var dynamicItem = item as dynamic;
return dynamicItem.FirstName == "Heines";

それ以外の場合は、匿名型へのキャストについてこの回答をチェックアウトできます-ただし、ポスターが言及しているように、これはおそらくほとんどのシナリオではあまり意味がありません:-)

https://stackoverflow.com/a/1409776/454272

于 2012-08-19T07:08:53.330 に答える
2

これはうまくいくはずです-

view.Filter = (item) =>
        {
             dynamic anyonymousItem = item;
             // Your condition here
             return anyonymousItem.CollegeID == 1;
        };
于 2012-08-19T08:02:18.370 に答える
1

私の意見では、ここで dynamc を使用することは最良の選択肢ではありません。プロパティ (Player など) を含むクラスを作成し、クエリを に変更する必要がありますselect new Player()。フィルターでは、オブジェクトがタイプPlayerであるかどうかを確認し、必要に応じてキャストできます。

このように、動的ディスパッチに依存する必要はなく、静的型付けのすべての利点を得ることができます (たとえば、プロパティ名を間違って記述したり、その名前を変更した場合のコンパイル時エラー)。

于 2012-08-19T09:18:01.787 に答える