4

私は3つのテーブルを持っています。2には、すべての一意のファイルを取得するために UNION を実行する必要があるファイルのリストが含まれています。次に、3番目のテーブルに対して左外部結合を実行して、3番目のテーブルのみにあり、他のテーブルには含まれていないすべてのファイルを検索します2.

UNION を実行するには、次のものが必要です。

var imageUnion = (from img in dc.ImageT1
                  select img.filename).Union(
                  from img in dc.ImageT2
                  select img.filename);

ここで、3 番目のテーブルでのみファイルを取得するには、次のように左外部結合を実行します。

var query = from image in dc.ImageT1
            join active in dc.ActiveImages on image.filename equals 
            active.filename into gj
            from subimage in gj.DefaultIfEmpty()
            where subimage.filename == null
            select new { image.filename, image.size };  

1 つのテーブルに対して単純に左外部結合を実行する方法は理解していますが、最初のクエリの結果セットを左外部結合に取得するにはどうすればよいですか? 基本的に、ImagesT1 に対して左外部結合を実行する代わりに、imageUnion の結果に対して実行したいと考えています。

ありがとう!

4

2 に答える 2

4

You need to select more than one property in your Union; The current result is IEnumerable<string> (assuming your filename is a string).

var imageUnion = (from img in dc.ImageT1
                  select new { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new { Filename = img.filename, Size = img.size });

Then you should be able to use it in the second query to replace dc.ImageT1.

Though thinking more on it, the Union may not work with 2 anonymous types; To support that, maybe it'd be worth defining a class that has only a Filename and Size?

public class TempImage
{
    public string Filename { get; set; }
    public int Size { get; set; }
}

var imageUnion = (from img in dc.ImageT1
                  select new TempImage() { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new TempImage() { Filename = img.filename, Size = img.size });
于 2009-03-18T16:08:15.340 に答える
1

画像テーブルの代わりに、最初のクエリから再度選択できるはずです。何かのようなもの:

var query = from image in imageUnion
            join active in dc.ActiveImages on image.filename equals 
            active.filename into gj
            from subimage in gj.DefaultIfEmpty()
            where subimage.filename == null
            select new { image.filename, image.size };

編集: imageUnion クエリを編集して、サイズとファイル名 (および最終クエリで必要なその他の列) を選択する必要もあります。

于 2009-03-18T15:36:04.357 に答える